How to index array with an array?

18 ビュー (過去 30 日間)
xiaojuezi
xiaojuezi 2021 年 12 月 8 日
コメント済み: xiaojuezi 2021 年 12 月 8 日
Hi I want to index an element in an array. The array u can be 1D, 2D or 3D, and I have an index id which is 1D, 2D or 3D. I'm currently doing:
u(id) = val;
This works fine in the 1D case. However, for 2D cases, where id = [row,column], this line doesn't set the 2D array correctly. For example:
u = [0 0 0;
0 0 0;
0 0 0];
% Access element at row 2, column 1
id = [2,1]
u(id) = 1; % Not always setting the desired element
Similarly in 3D, id = [widthId, heightId, depthId].
Is there a way to solve this?
Thank you very much!
  1 件のコメント
James Tursa
James Tursa 2021 年 12 月 8 日
Please give a small numerical example of exactly how your 2D and 3D indexes are stored.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2021 年 12 月 8 日
編集済み: Stephen23 2021 年 12 月 8 日
u = [11,12,13;21,22,23;31,32,33]
u = 3×3
11 12 13 21 22 23 31 32 33
% Access element at row 2, column 1
id = [2,1]
id = 1×2
2 1
ic = num2cell(id);
u(ic{:})
ans = 21
  1 件のコメント
xiaojuezi
xiaojuezi 2021 年 12 月 8 日
Thank you very much!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by