Replacing values in a 4D array.
9 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!