How to find the row and column for a value within a matrix [c] nearest or equal to 700.
1 回表示 (過去 30 日間)
古いコメントを表示
Attempts so far.
for inputMat = [c];
[row,col] = find (c==700);
end
inputMat = [c];
[myRow, myCol] = find(inputMat (closest(700)));
numericalAns = [myRow myCol];
0 件のコメント
採用された回答
John D'Errico
2021 年 4 月 20 日
編集済み: John D'Errico
2021 年 4 月 20 日
Easy. And there would be many ways to do it.
c = rand(5,5)*1000
[~,ind] = min(abs(c(:) - 700))
[rowind,colind] = ind2sub(size(c),ind)
So the closest element to 700 lives in row 5, column 1.
c(rowind,colind)
その他の回答 (1 件)
Jan
2021 年 4 月 20 日
編集済み: Jan
2021 年 4 月 21 日
find(inputMat (closest(700)))
This is pure guessing. Notice that a command like closest(X) cannot even work in theory, because you provide one input only. So if this command exists, Matlab would ask: "closest to what?"
for inputMat = [c];
This is not the way Matlab's FOR loop works.
Guessing is no successful strategy for such a powerful tool as Matlab. Please read the GEtting Started chapters of the documentation and read Matlab's Onramp: https://www.mathworks.com/learn/tutorials/matlab-onramp.html
[value, index] = min(abs(c(:) - 700))
[row, col] = find(c == c(index)) % [EDITED, Typo fixed]
% Replies multiple matchs, if there are some
2 件のコメント
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!