user3698120
user3698120

Reputation: 61

Matlab - elmination of rows in array that is not needed

I am comparing a 100 x 1 called "x" with the value from a 4 x 1 matrix called "xCP". I need a finalMatrix with only the values in x that are higher than the third value from xCP. However, the code i have below provides me with a 100 x 1 matrix with zeros in rows that the value are not higher. I only want the 25(e.g.) rows in finalMatrix that have the higher value. So my finalMatrix needs to be a 25 x 1 with values instead of 100 x 1 with 25 values and 75 zeros.

This is what i have so far:

K = size (x)
length = K(1)
finalMatrix = zeros(length,1);

count = 1;
for i=1:length;
    if x(count,1) >= xCP(3)
    finalMatrix(count,1) = x(count,1);
    end
    count =count+1;
end

Thank you!

Upvotes: 0

Views: 36

Answers (1)

John Alexiou
John Alexiou

Reputation: 29244

You can add finalMatrix(count+1:length,1) = [] in the end of the code. It will remove the excess elements.

Upvotes: 1

Related Questions