find the position of all non-zero minimum values in each column of a matrix

10 ビュー (過去 30 日間)
Giannis Nikolopoulos
Giannis Nikolopoulos 2021 年 5 月 4 日
コメント済み: Walter Roberson 2021 年 5 月 6 日
Hello,
I need your help with the below problem:
I have a matrix something like that:
a = [2 0 3;1 2 5;0 0 0;1 3 3;0 0 6;0 2 7;1 0 0]
and i need to find the position of all minimum (non-zero) values of each column. For example in the first column the position of the three ones, in the second column the position of the two two etc...
I think that it is very easy but I'm stuck
Thanks in advance

回答 (4 件)

Bruno Luong
Bruno Luong 2021 年 5 月 5 日
編集済み: Bruno Luong 2021 年 5 月 5 日
a = [2 0 3;1 2 5;0 0 0;1 3 3;0 0 6;0 2 7;1 0 0]
b=a;
b(b==0)=NaN;
[r,c]=find(b==min(b,[],1));
rmin=accumarray(c(:),r(:),[size(a,2),1],@(r){r});
if ~iscell(rmin) % exceptional case where a contains all 0
rmin = cell(size(rmin));
end
celldisp(rmin)

Walter Roberson
Walter Roberson 2021 年 5 月 4 日
a = [2 0 3;1 2 5;0 0 0;1 3 3;0 0 6;0 2 7;1 0 0]
a = 7×3
2 0 3 1 2 5 0 0 0 1 3 3 0 0 6 0 2 7 1 0 0
positions = cellfun(@(A) find(A==min(A)), num2cell(a,1), 'uniform', 0)
positions = 1×3 cell array
{3×1 double} {4×1 double} {2×1 double}
The result has to be a cell array because there are a different number of matches in each column.
  8 件のコメント
Walter Roberson
Walter Roberson 2021 年 5 月 6 日
find is a variable.
Do not assign to a variable named find
Walter Roberson
Walter Roberson 2021 年 5 月 6 日
a = zeros(3);
positions = cellfun(@(A) find(A==reshape(min(A(A~=0)),1,[])), num2cell(a,1), 'uniform', 0)
positions = 1×3 cell array
{0×1 double} {0×1 double} {0×1 double}
celldisp(positions)
positions{1} = [] positions{2} = [] positions{3} = []

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


Image Analyst
Image Analyst 2021 年 5 月 4 日
If you want a lengthier, but less cryptic brute force way of finding the min in each column and the row(s) it occurs at, see this:
a = [2 0 3;1 2 5;0 0 0;1 3 3;0 0 6;0 2 7;1 0 0]
[rows, columns] = size(a)
minOfEachColumn = zeros(columns, 1);
indexesOfEachColumn = cell(columns, 1);
for col = 1 : columns
thisColumn = a(:, col);
% Ignore zeros by setting them to inf.
thisColumn(thisColumn == 0) = inf;
% Get the min value of what's left.
minOfEachColumn(col) = min(thisColumn);
fprintf('For column #%d, the min non-zero value is %f.\n', col, minOfEachColumn(col));
% Get what index(es) that that min occurs at.
indexesOfEachColumn{col} = find(thisColumn == minOfEachColumn(col));
end
minOfEachColumn % Display in command window.
celldisp(indexesOfEachColumn)

Giannis Nikolopoulos
Giannis Nikolopoulos 2021 年 5 月 5 日
Hello,
Thank you all (@Image Analyst, @Bruno Luong, @Walter Roberson) for your help. All suggestions are good for me with the prefered results.
@Walter Roberson I restarted my matlab and the result were fine then!!
Best,
Giannis
  1 件のコメント
Image Analyst
Image Analyst 2021 年 5 月 5 日
That should not have been needed. Worst case, you would have just had to do a "clear all". I think your "a" was not what you thought it was.

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

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by