Replacing values in a 4D array.

10 ビュー (過去 30 日間)
mathman
mathman 2017 年 12 月 25 日
コメント済み: mathman 2017 年 12 月 26 日
This is a question arising from this thread: https://www.mathworks.com/matlabcentral/answers/374453-round-number-closest-to-0-in-array But as it's a distinctly different question to the original post I thought to make another thread. However, if this is an issue please let me know.
I've indexed a 4D array,F, to locate the smallest values in each column of F using:
[~, idx] = min(abs(F(:,:,:,:)));
This gives me an array, 'idx', which contains the POSITIONS of the smallest elements in each column of F. For example,
F = [5 3 6; 4 6 1; 9 5 4] means idx = [2 1 2]. I then tried to replace these identified elements using
F(idx) = 0;
However, it's only replacing the first 5 elements of the first column of F with 0. I think this is because as F is a 4D array, when indexing it the elements are listed in a single column where the element positions are 1,2,3,4,5,6 etc... So the values identified in idx range from 1-5. So I think it's only replacing the first 5 elements of F as defined in the single column, rather than each individual column as I want it to.
So the question is how would I go about replacing the elements in each column of F as identified by idx.

採用された回答

Matt J
Matt J 2017 年 12 月 26 日
編集済み: Matt J 2017 年 12 月 26 日
So the question is how would I go about replacing the elements in each column of F as identified by idx.
The elements refered to by idx are only the first occurraence of the minima in each column. If you really want all occurrences replaced do,
F(F==min(F,[],1))=0; %assumes R2016b or higher. Otherwise, use bsxfun
  1 件のコメント
mathman
mathman 2017 年 12 月 26 日
Thanks a lot that worked really well!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by