Drop a column from a matrix if one number in that column is less than 30
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Xiaoyu Ma
el 16 de En. de 2021
Comentada: Xiaoyu Ma
el 16 de En. de 2021
Hi everyone:
I'm new to matlab. As the title describes, I am trying to drop all columns in a matrix which contain a number less than 30. I am now using a loop to do so, but as the matrix grow bigger it become very slow, so I wonder if there's a more efficient way of coding to achieve the same effect. Below is my current code.
for i = 1:npaths
if sum(bigMatrix(:,i)<30)>0
bigMatrix(:,i)=[];
end
end
Many thanks!
0 comentarios
Respuesta aceptada
dpb
el 16 de En. de 2021
VAL=30; % don't bury "magic numbers" in code; use variables
bigMatrix=bigMatrix(:,all(bigMatrix>=VAL)); % use MATLAB logical addressing/indexing
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!