How to find the row and column for a value within a matrix [c] nearest or equal to 700.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Alex Hinchcliffe
el 20 de Abr. de 2021
Comentada: Jan
el 21 de Abr. de 2021
Attempts so far.
for inputMat = [c];
[row,col] = find (c==700);
end
inputMat = [c];
[myRow, myCol] = find(inputMat (closest(700)));
numericalAns = [myRow myCol];
0 comentarios
Respuesta aceptada
John D'Errico
el 20 de Abr. de 2021
Editada: John D'Errico
el 20 de Abr. de 2021
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)
Más respuestas (1)
Jan
el 20 de Abr. de 2021
Editada: Jan
el 21 de Abr. de 2021
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 comentarios
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!