Matlab if statements and extracting rows of data

  • MATLAB
  • Thread starter joanne34567
  • Start date
  • Tags
    Data Matlab
In summary, the person is trying to find a way to extract specific numbers from a matrix of data. They are looking for a solution that is easy to follow and that they can use again later. They have tried different things, but cannot seem to get a result. They find a helpful page that tells them how to use if statements. Finally, the person thanks the person who asked the question and provides a summary of their findings.
  • #1
joanne34567
12
0
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?
 
Physics news on Phys.org
  • #2
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.
 
  • #3
You are correct
Thank you :-)
 
Last edited:
  • #4
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
 
  • #5
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
 
  • #6
% 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!
 
  • #7
yes, this definitely helps !
thanks a lot !
 

1. What is the syntax for writing an if statement in Matlab?

The syntax for writing an if statement in Matlab is: if condition code to execute if condition is trueend

2. How do you use logical operators in Matlab if statements?

You can use logical operators such as && (AND) and || (OR) in Matlab if statements to combine multiple conditions. For example: if (x > 5 && y < 10) % code to execute if both conditions are trueend

3. Can you have multiple if statements within one if statement in Matlab?

Yes, you can have multiple if statements within one if statement in Matlab by using the elseif keyword. For example: if x > 10 % code to execute if x is greater than 10elseif x < 5 % code to execute if x is less than 5else % code to execute if neither condition is metend

4. How do you extract specific rows of data from a matrix in Matlab?

You can use logical indexing to extract specific rows of data from a matrix in Matlab. For example, if you have a matrix A and you want to extract all rows where the first column is greater than 10, you can use the syntax A(A(:,1) > 10,:).

5. Is there a way to execute different code depending on the number of conditions met in an if statement in Matlab?

Yes, you can use the switch statement in Matlab to execute different code based on the number of conditions met. This allows for more flexibility than using multiple elseif statements. For example: switch x case 1 % code to execute if x equals 1 case 2 % code to execute if x equals 2 otherwise % code to execute if x does not equal 1 or 2end

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
1
Views
638
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
Back
Top