Extract specific values from a 3D matrix?

2 ビュー (過去 30 日間)
Ivan Gufler
Ivan Gufler 2023 年 1 月 31 日
編集済み: Fangjun Jiang 2023 年 1 月 31 日
Hello,
I have a 3d matrix (call it A) of the following dimension: 9000, 9, 2. From this matrix I want to update two values in the following positions:
row_index=[4000, 3000]
col_index=[4,5]
Basically I want:
A(row_index, col_index,:)=B
where B is a 2x1 vector.
I could do this within a loop but I'd like to vectorize this operation.
Does anybody have a suggestion on how to do this in an efficient way?

回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2023 年 1 月 31 日
編集済み: Fangjun Jiang 2023 年 1 月 31 日
A(sub2ind(size(A),row_index,col_index))=B
For example
A=false(6,6,6);
row_index=[4, 3];
col_index=[4,5];
B=[true;true];
A(sub2ind(size(A),row_index,col_index))=B;
sum(A,'all')
ans = 2

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by