Drop a column from a matrix if one number in that column is less than 30

1 view (last 30 days)
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!

Accepted Answer

dpb
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)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!