第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將矩陣插入另一個矩陣(特定位置)

將矩陣插入另一個矩陣(特定位置)

慕容3067478 2022-10-05 17:52:48
我對矩陣加法有疑問。我有一個矩陣 A = np.ones([10,10]) 和一個矩陣 B = np.array([[2,2,2],[2,2,2],[2,2,2]] )?,F(xiàn)在我想將矩陣 B 添加到 A 但在特定位置,第 2,6,7 行和第 2,6,7 列。我應該怎么做才能得到以下矩陣:[[1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1], [1,1,3,1,1,1,3,3,1,1], [1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1], [1,1,3,1,1,1,3,3,1,1], [1,1,3,1,1,1,3,3,1,1], [1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1]]我更習慣于 Matlab,它看起來像這樣: A((3,7,8),(3,7,8)) = A((3,7,8),(3,7 ,8)) + B. 我在 Python 中嘗試過類似的東西,但尺寸不匹配。
查看完整描述

1 回答

?
炎炎設計

TA貢獻1808條經(jīng)驗 獲得超4個贊

這是一種方法:


Python 中的多維索引要求您顯式索引每個單元格。所以需要先創(chuàng)建索引,然后使用如下:


ind = np.array([[2,6,7]])   # Notice the 2D array

rows = np.broadcast_to(ind.transpose(), (3,3))

cols = np.broadcast_to(ind, (3,3))

A[rows, cols]+=B  # A cell from rows matrix and a corresponding cell in cols matrix together form one cell index.

輸出:


array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

   [1, 1, 3, 1, 1, 1, 3, 3, 1, 1],

   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

   [1, 1, 3, 1, 1, 1, 3, 3, 1, 1],

   [1, 1, 3, 1, 1, 1, 3, 3, 1, 1],

   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])

請閱讀:https ://docs.scipy.org/doc/numpy-1.13.0/user/basics.indexing.html


出于某種原因,雖然以下確實從中挑選出正確的矩陣A,但分配給它不起作用:


ind_1 = np.array([2,6,7])

A[ind_1,:][:, ind_1] = B # No error, but assignment does not take place


查看完整回答
反對 回復 2022-10-05
  • 1 回答
  • 0 關(guān)注
  • 77 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號