[英]Matlab how to 'sort' the elements of an entire cell array and access the largest values
So I am aware that using the sort(array, 'descend') function in Matlab will sort an array into descending order, with the return result being [values, indices]. 因此,我知道在Matlab中使用sort(array,'descend')函数会将数组按降序排序,返回结果为[值,索引]。
I need to get the largest values from an entire cell array, edit them, then place them back into their original places. 我需要从整个单元格数组中获取最大值 ,对其进行编辑,然后将其放回原始位置。
For example: 例如:
%dst is a 1x5 cell
d1=dst{1};
d2=dst{2};
d3=dst{3};
d4=dst{4};
d5=dst{5};
%d1-d5 are all 512x512x16 double
I can use [d1SortedValues, d1SortedIndices] = sort(d1(:),'descend');
我可以使用
[d1SortedValues, d1SortedIndices] = sort(d1(:),'descend');
to get the sorted array and indices for each double cell array d1-d5 BUT I need to do something equivalent to [dstSortedValues, dstSortedIndices] = sort(dst(:),'descend');
要获取每个双单元格数组d1-d5的排序数组和索引,但我需要做一些等效于
[dstSortedValues, dstSortedIndices] = sort(dst(:),'descend');
so that I can access the highest magnitude elements. 这样我就可以访问最大幅度的元素。
ie for each double array I can do the following to access the highest value in d1. 即对于每个双精度数组,我可以执行以下操作以访问d1中的最大值。
d1(d1SortedIndices(1)) = d1(d1SortedIndices(1)) + value;
How to get the highest values in the entire 1x5 cell? 如何获得整个1x5单元中的最大值?
Thanks! 谢谢!
Edit based on new information you put in comments: 根据您在评论中添加的新信息进行编辑:
If you want to find the position of N
largest values and replace or modify them, the ind2sub
function can help you greatly. 如果要查找
N
最大值的位置并替换或修改它们, ind2sub
函数可以为您提供很大帮助。 Basically: 基本上:
N
max elements) N
最大元素) It looks like that: 看起来像这样:
nMaxVal2find = 30 ; %// how many largest element N to locate/modify
fullArrayC = cellfun(@(a) a(:), dst , 'uni',0) ; %// cell array of linearised vectors
fullArray = cat(1, fullArrayC{:} ) ; %// linear array of all the values
[sVals,sInds] = sort(fullArray,'descend') ; %// sort the full array
%// Keep only N largest values
Val2keep = sVals(1:nMaxVal2find) ;
ind2keep = sInds(1:nMaxVal2find) ;
%// find all the "in cell" position of the N largest elements
[ii,jj,kk,icell] = ind2sub( [size(dst{1}) numel(dst)] , ind2keep ) ;
%// loose loop to replace elements / can be improved
for idx=1:numel(icell)
%// dst{ic(iCell)}(ii(iCell),jj(iCell),kk(iCell)) = dst{ic(iCell)}(ii(iCell),jj(iCell),kk(iCell)) * 10 ;
dst{icell(idx)}(ii(idx),jj(idx),kk(idx)) = Val2keep(idx) * 10 ;
end
If you want to find the highest value ( meaning you are sure to have only one largest value ) of your whole cell array, you can directly get the index with max
. 如果要查找整个单元格数组的最大值 ( 意味着您肯定只有一个最大值 ),则可以直接使用
max
获得索引。
To use that on a cell array you can use cellfun
. 要在单元阵列上使用它,可以使用
cellfun
。 Once located, you can modify the value directly in place (instead of having to reorder your arrays twice). 找到后,您可以直接在适当位置修改值(而不必两次重新排列数组)。
%// find the maximums of each cell, and their linear index
[maxVals,maxInds] = cellfun( @(a) max(a(:)) , dst(:) , 'uni',0) ;
%// find the absolute maximum and in which cell it was
[maxV,maxCellInd] = max( cell2mat(maxVals) ) ;
%// return the (i,j,k) style index of the maximum value
[ii,jj,kk] = ind2sub( size(dst{maxCellInd}), maxInds{maxCellInd} ) ;
%// modify the value in place
dst{maxCellInd}(ii,jj,kk) = dst{maxCellInd}(ii,jj,kk) * 10 ; %// the value times 10
Notes: 笔记:
max
will only return the index of the first element. max
将仅返回第一个元素的索引。 sort
, you can also use that with cellfun
on the whole cell array. sort
,也可以在整个单元阵列cellfun
它与cellfun
一起使用。
Last note: I used that code to generate sample data: 最后说明:我使用该代码生成示例数据:
for ic=1:5
dst{ic} = rand(5,5,3) ;
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.