Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab if statements and extracting rows of data

  1. Oct 1, 2011 #1
    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?
  2. jcsd
  3. Oct 1, 2011 #2


    User Avatar
    Science Advisor

    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.
  4. Oct 1, 2011 #3
    You are correct
    Thank you :-)
    Last edited: Oct 1, 2011
  5. Sep 17, 2012 #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
  6. Sep 17, 2012 #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
  7. Sep 19, 2012 #6
    % Ok, so say your matrix looks like this:


    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:


    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:


    % 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):


    % Hope this helps!!
  8. Sep 19, 2012 #7
    yes, this definitely helps !
    thanks a lot !
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook