Drop a column from a matrix if one number in that column is less than 30
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Xiaoyu Ma
am 16 Jan. 2021
Kommentiert: Xiaoyu Ma
am 16 Jan. 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 Kommentare
Akzeptierte Antwort
dpb
am 16 Jan. 2021
VAL=30; % don't bury "magic numbers" in code; use variables
bigMatrix=bigMatrix(:,all(bigMatrix>=VAL)); % use MATLAB logical addressing/indexing
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!