Logical indexing in multidimensional array

9 ビュー (過去 30 日間)
J B
J B 2021 年 2 月 9 日
編集済み: Jan 2021 年 2 月 10 日
I have a 4-dimensional array and would like to replace parts of it with NaN values. The array has dimensions 60 x 321 x 28 x 16, the first three dimensions are some parameters I iterated through, the last dimension is the actual results I obtained. I would now like to replace some of the results, depending on the value of one of the elements from the results. I tried this:
res = % some 4D array (60 x 231 x 28 x 16)
res(res(:,:,:,9) > 5, 4:16) = NaN; % Fail
What I want to obtain would be something like this:
for x=1:60
for y=1:321
for z=1:28
if res(x,y,z,9) > 5
res(x,y,z,4:16) = NaN;
end
end
end
end
Is there a more compact way of writing this?
  1 件のコメント
Jan
Jan 2021 年 2 月 9 日
編集済み: Jan 2021 年 2 月 10 日
This is a good question. To my surprise I did not even find a built-in function for this. I'd expect a function for masking [M x N x 3] arrays (RGB-images) in the image processing toolbox.

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

回答 (1 件)

Matt J
Matt J 2021 年 2 月 9 日
編集済み: Matt J 2021 年 2 月 9 日
T=reshape(res,[],16);
T(T(:,9)>5,4:16)=nan;
res=reshape(T,size(res));

カテゴリ

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by