フィルターのクリア

Access Elements of a Matrix, via an array of varying size

4 ビュー (過去 30 日間)
Ayden Clay
Ayden Clay 2020 年 4 月 2 日
コメント済み: Ameer Hamza 2020 年 4 月 2 日
Hello, I would like to access elements of an N-D matrix, by inputting an Nx1 vector as it's element.
Currently, I have an array that contains the indices that I would like, for example:
idx = [1,2,3,4,5];
a = data.set(1,2,3,4,5);
Howevever, the size of idx is variable based on the dimensions of the matrix, and I can't seem to figure out how to do this without hard-coding it up. I've looked into the sub2ind function, but don't believe that it will serve the purpose I need.
Can anybody help?
  9 件のコメント
Ayden Clay
Ayden Clay 2020 年 4 月 2 日
Perfect, thank you very much!
Ameer Hamza
Ameer Hamza 2020 年 4 月 2 日
Adam, agree. I, too, use idx too commonly for subscripts. I realized this while writing the answer that it can create confusion sometimes.

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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 2 日
編集済み: Ameer Hamza 2020 年 4 月 2 日
I believe you should be able to use sub2ind for this purpose, but here is a simpler solution with cell array
A = rand(4,5,10,20); % a 4x5x10x20 matrix
Subs = {1, 3, 5, 10};
required_element = A(Subs{:});
For you example
Subs = [1,2,3,4,5];
Subs = num2cell(idx);
a = data.set(Subs{:});
  3 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 4 月 2 日
Glad to be of help.
Ayden Clay
Ayden Clay 2020 年 4 月 2 日
Worked flawlessly, thank you so much for your help!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by