Drop a column from a matrix if one number in that column is less than 30
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
dpb
on 16 Jan 2021
VAL=30; % don't bury "magic numbers" in code; use variables
bigMatrix=bigMatrix(:,all(bigMatrix>=VAL)); % use MATLAB logical addressing/indexing
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!