フィルターのクリア

sub2ind with 3D matrix?

4 ビュー (過去 30 日間)
MementoMori
MementoMori 2023 年 4 月 4 日
編集済み: DGM 2023 年 4 月 4 日
given a code, for example, like this
A=rand(10,10);
indX(:,:,1)=[2,3;1,6]; indX(:,:,2)=[2,5;2,6];
indY(:,:,1)=[4,6;2,3]; indY(:,:,2)=[7,5;3,4];
for i=1:2
for j=1:2
ind=sub2ind(size(A), indX(:,:,i), indY(:,:,j));
figure,imagesc(A(ind));
end
end
I would like to obtain the same result, without using a for loop. Is there any faster way?
  2 件のコメント
MarKf
MarKf 2023 年 4 月 4 日
So you want to select subsets of a matrix without a loop given 2x2 sets of indices, is that correct? Then not sure why the 3D matrix is important (they could be in cells for example), but maybe you started with it and you need to put them in indices.
That would be assuming you are using imagesc just to show the results (but then why not use disp(A(ind)) or fprintf('%d %d\n%d %d\n\n', A(ind)') or even just A(ind) without semicolon for that). Otherwise, if you need to plot, the loop is the most straightforward solution.
Since you're combining indices sets, (x1-y1,1-2,2-1,2-2 combinations) and you'd need to assign the various sub-matrices to different variables or cells or do some operations with them anyway (like imagesc above), the loop would be again still the fastest solution.
DGM
DGM 2023 年 4 月 4 日
編集済み: DGM 2023 年 4 月 4 日
The faster way would be to not generate a bunch of figures in a loop. If you don't need them, don't spend time on them. If you might need them, conditionally generate them only when requested.

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by