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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by