How can I compare all row in a column with a value and replace it?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
bkshn
el 17 de Jun. de 2015
Respondida: Walter Roberson
el 17 de Jun. de 2015
Hello
I want to compare all row in one column with a value, and if one of them being larger than value, I want replace it with value.
How can I do this with if statement or any other way ?
if(path(1,:)>value)
? ???
0 comentarios
Respuesta aceptada
Walter Roberson
el 17 de Jun. de 2015
is_greater = path(1,:) > value;
Now if you want to replace the entire column with the value then:
if any(is_greater)
path(1,:) = value;
end
If you want to replace only the locations then
path(1,is_greater) = value;
However, there is another way for the situation where you want to take the current item provided it is not greater than value, and value otherwise: you can use
path(1,:) = min(path(1,:), value);
and you can do the entire array at once with
path = min(path, value);
When you use min() the replacement is always the cutoff value. When you use the logical indexing I showed, the replacement could be arbitrary, such as
path(1,is_greater) = 0;
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!