# Matlab if statements and extracting rows of data

• MATLAB
joanne34567
There may be a simple solution (I'm guessing so), but I've been trying for ages now and can't seem to figure it out... I have a matrix of data in Matlab, 4 columns by 1000 rows. I need to evaluate that data...
I need to find answers in column 3 which lie between 0.9998 and 1. I want to extract all of these numbers, but keep them with columns 1 - 4 of my data... Column 3 represents the R^2 value and I want a new table where the R^2 value is over 0.9998, and need to extract the corresponding figures from the other columns, so I keep my 4 columns, but have less then the 1000 rows...
I've tried using if statements, but can't seem to get a result out...
Can anyone help?

## Answers and Replies

It's a bit quirky looking, but say your matrix is called "M", then one way would be :

M( M(:,3) >= 0.9998 , : )

I'm assuming the data in column 3 is some sort of coefficient with a maximum value of 1, is that correct? Otherwise you'd have to test for <= 1 as well.

joanne34567
You are correct
Thank you :-)

Last edited:
karna87
I was searching for a similar solution and found this page. this was an awesome help to me. I am super grateful to "uart"

thanks a lot. thanks also to the person who asked the question

Also, I was curious if you would recommend a site where I could learn such neat tricks

karna87
I have files that look like following:

% num vel error

1 4 0.12
2 6 1.23
..

I want to extract "rows" according to the value of errors. for example, I only want raws which have low velocity errors. I will then use them to compute average-velocity. Can you please help what's the best way to proceed

joanne34567
% Ok, so say your matrix looks like this:

x=[1,2,0.1;3,4,0.5;5,6,0.9;7,8,0.76;9,10,1.2]

x =

1.0000 2.0000 0.1000
3.0000 4.0000 0.5000
5.0000 6.0000 0.9000
7.0000 8.0000 0.7600
9.0000 10.0000 1.2000

% Define what you want to say is low velocity error (so <=0.8 for example as I'm not sure of your units)

good=(x(:,3)<=0.8) %based on % num vel error (as you mentioned in your question with col 3= velocity error - if velocity error is in a different column then substitute the 3 in (:,3))

% This defines a new variable "good" which is a logical statement which gives a value of 1 the rows where velocity error is <=3 - basically puts your data into a binary

% to get the rows out and keep the matrix you just type:

x(good,:)

ans =

1.0000 2.0000 0.1000
3.0000 4.0000 0.5000
7.0000 8.0000 0.7600

% or call it something new like:

y=x(good,:)

% You could also just do it like this to avoid creating the "good" binary code... this just joins those lines of code into 1 (but I think it helps illustrate the point showing it in both ways):

y=x(x(:,3)<=0.8,:)

% Hope this helps!!

karna87
yes, this definitely helps !
thanks a lot !