我有一個矩陣數字,其中包含
0.2728 0.2304 0.2008 0.1900 0.2008 0.2304 0.2728
0.2304 0.1786 0.1391 0.1233 0.1391 0.1786 0.2304
0.2008 0.1391 0.0843 0.0567 0.0843 0.1391 0.2008
0.1900 0.1233 0.0567 0.0100 0.0567 0.1233 0.1900
0.2008 0.1391 0.0843 0.0567 0.0843 0.1391 0.2008
0.2304 0.1786 0.1391 0.1233 0.1391 0.1786 0.2304
0.2728 0.2304 0.2008 0.1900 0.2008 0.2304 0.2728
我試圖找到最小值(或如果有相等的分鐘值)。我努力了
[min_val,idx]=min(數字);
[行,列]=ind2sub(大小(數字),idx);
我得到的第 4 行是正確的,但第 1 列顯然不是最小值,最小值在中間。當我列印 min(number) 時,我給出了第 4 行的全部內容,所以我也嘗試了
[min_val,idx]=min(min(number));
[行,列]=ind2sub(大小(數字),idx);
但我給出了相同的結果。我不太確定這里發生了什么。任何幫助,將不勝感激!
用于獲取多個最小值位置的代碼。
[min_val, idx] = min(number(:));%finds minimum value of n
mins = number==min_val;%logical array that gives 1 where number is at
its minimum
ind1 = zeros();
ind2= zeros();
for i = 1:length(x)
for j = 1:length(y)
if min_val(i,j) == 1
ind1 = [ind1;i];% indcies where mins = 1
ind2 = [ind2;j];
end
end
end
ind1 = ind1(ind1~=0);
uj5u.com熱心網友回復:
看起來您可以使用min并ind2sub實作預期的輸出:
matlab:1> number = [0.2728 0.2304 0.2008 0.1900 0.2008 0.2304 0.2728; 0.2304 0.1786 0.1391 0.1233 0.1391 0.1786 0.2304; 0.2008 0.1391 0.0843 0.0567 0.0843 0.1391 0.2008; 0.1900 0.1233 0.0567 0.0100 0.0567 0.1233 0.1900; 0.2008 0.1391 0.0843 0.0567 0.0843 0.1391 0.2008; 0.2304 0.1786 0.1391 0.1233 0.1391 0.1786 0.2304; 0.2728 0.2304 0.2008 0.1900 0.2008 0.2304 0.2728]
number =
0.272800 0.230400 0.200800 0.190000 0.200800 0.230400 0.272800
0.230400 0.178600 0.139100 0.123300 0.139100 0.178600 0.230400
0.200800 0.139100 0.084300 0.056700 0.084300 0.139100 0.200800
0.190000 0.123300 0.056700 0.010000 0.056700 0.123300 0.190000
0.200800 0.139100 0.084300 0.056700 0.084300 0.139100 0.200800
0.230400 0.178600 0.139100 0.123300 0.139100 0.178600 0.230400
0.272800 0.230400 0.200800 0.190000 0.200800 0.230400 0.272800
matlab:2> [min_val, idx] = min(number(:))
min_val = 0.010000
idx = 25
matlab:3> [row, col] = ind2sub(size(number), idx)
row = 4
col = 4
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/406940.html
標籤:
上一篇:在檔案中批量輸出變數
