Conditional calculations on a numeric matrix
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Wah On Ho
el 18 de Nov. de 2019
Comentada: Walter Roberson
el 18 de Nov. de 2019
Hi All,
I'm relatively new to Matlab and I find myself slightly stuck. I have a numeric matrix of numbers comprising of floating point values. Depending on whether the value in each cell is less than or greater than some threshold value, either one or another calculation is made.
Below is a much simplified expression of my problem but it illustrates the form of what I'm trying to do with millions of cells of values which takes a rather longer time than the illustration below.
I realise that I can make a logic matrix from the input matrix but then I'm stuck on what follows to complete the calculations.
Any advice is gratefully received.
input = magic(10);
thresholdvalue = 50;
for col = 1 : 10
for row = 1 : 10
if input(row,col) > thresholdvalue
result(row,col) = input(row,col)*2;
else
result(row,col) = input(row,col)/2;
end
end
end
0 comentarios
Respuesta aceptada
Walter Roberson
el 18 de Nov. de 2019
mask = input > thresholdvalue;
result(mask) = input(mask) * 2;
result(~mask) = input(mask) / 2;
2 comentarios
Walter Roberson
el 18 de Nov. de 2019
Ah, yes, sorry, I did have a typo there.
result(~mask) = input(~mask) / 2;
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!