Matlab if statements and extracting rows of data

  • Context: MATLAB 
  • Thread starter Thread starter joanne34567
  • Start date Start date
  • Tags Tags
    Data Matlab
Click For Summary

Discussion Overview

The discussion revolves around extracting specific rows from a matrix in Matlab based on conditions applied to certain columns. Participants explore methods for filtering data, particularly focusing on values in specific columns that meet defined criteria, such as R^2 values and velocity errors.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant seeks assistance in extracting rows from a 4-column matrix where the R^2 value in column 3 is between 0.9998 and 1.
  • Another participant suggests using a logical indexing approach in Matlab to extract the desired rows, assuming the data in column 3 is a coefficient with a maximum value of 1.
  • A later reply confirms the assumption about the data in column 3.
  • Another participant shares a similar problem regarding extracting rows based on low velocity errors and requests guidance on the best approach.
  • A participant provides a detailed example of how to filter rows based on a condition applied to a column, demonstrating the use of logical indexing in Matlab.
  • One participant expresses gratitude for the provided solutions and asks for recommendations on resources to learn more about such techniques.
  • Another participant confirms that the provided solution is helpful.

Areas of Agreement / Disagreement

Participants generally agree on the methods for extracting rows based on conditions, with some variations in the specific criteria and examples discussed. No consensus on a single method is established, as multiple approaches are presented.

Contextual Notes

Some assumptions about the data structure and the definitions of "low velocity error" remain unspecified, and participants do not resolve these details.

joanne34567
Messages
12
Reaction score
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
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.
 
You are correct
Thank you :-)
 
Last edited:
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
 
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
 
% 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!
 
yes, this definitely helps !
thanks a lot !
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K