finding the exact coordinates of the minimum value in a cell array
3 次查看(过去 30 天)
显示 更早的评论
mn = cellfun(@(x) min(x(x>0)),A,'Un',0);
[mn,idx] = min(fliplr([mn{:}]));
mn % Show the minimum positve value.
idx = length(A) - idx + 1 % Which cell has the min.
L = cellfun(@(x) find(x==mn),A,'Un',0);
L = L{idx} % And the positions.
I am trying to find the coordinates with the above formula in a cell array 100x61 but each time I get a value that is negative. idx for instance turns negative and this does not help me to locate the coordinates of the minimum value in that cell array. Can anyone help please
2 个评论
Stephen23
2014-12-22
Why are you using a cell array? Without knowing what A looks like, it is difficult to make any suggestions about fixing this...
采纳的回答
Stephen23
2014-12-22
编辑:Stephen23
2014-12-22
Unless you have good reasons to use cell arrays, then the simplest answer is to store your data in one numeric array and use the standard MATLAB command min . If the data is of different lengths, you can pad the rows/columns/etc with NaN's, and use nanmin . Both of these will be a million times faster and neater than playing around with cell arrays.
Previous questions you have asked on MATLAB Answers have shown an over-reliance on cell arrays, when using numeric arrays would make your own life so much easier. Cell arrays are not mandatory for MATLAB coding, and MATLAB works best when using vectorized code directly on numeric arrays. Please consider this carefully!
As I understand it, you are wanting to locate the minimum positive value of all numeric arrays that are contained in one cell array, and to know its indices. This is just an exercise in keeping track of indices:
A = num2cell(reshape(randperm(12)-6,[],2),2);
% Indices of positive values:
V = cellfun(@(a)find(a>0), A, 'UniformOutput',false);
% Exclude cells with all(<0):
W = find(~cellfun('isempty',V));
% Locate minimum for each cell, then of all cells:
[X,Y] = cellfun(@(a,v)min(a(v)), A,V, 'UniformOutput',false);
[a1,Z] = min([X{W}]);
% Index of the cell containing the minimum:
x1 = W(Z);
% Index of the minimum in the numeric array in that cell:
c1 = V{x1}(Y{x1});
% The extracted value should be the same as <a1>:
A{x1}(c1)
As you are not very specific about the exact problem, I have assumed that A is a vector, that the minimum positive value occurs only once, and that there is always at least one positive value in at least one numeric array.
Of course doing this with a simple numeric array makes your life much easier, as you could do something like this:
% Merge fake data into one numeric array:
E = vertcat(A{:});
% Locate minimum:
F = find(E>0);
[a2,G] = min(E(F));
[r2,c2] = ind2sub(size(E),F(G));
Note that the indices are the same: c1==c2, x1==r2, and a1==a2.
5 个评论
Stephen23
2015-1-8
编辑:Stephen23
2015-1-8
You can use sort instead of min, and select the first N values that it returns (instead of the one value that min returns). Note that you will have to collect vectors of values (and their associated indices), instead of the simple scalars that are required to solve your original question.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!