MATLAB Answers

How to exclude certain rows and columns based on a column value?

조회 수: 30(최근 30일)
Dear all,
Attached you find a data file called "a" that has 30 rows and 24 columns. What I call "a trial" represents 1 row of "a" that includes all 24 columns, i.e. a trial refers to a(1,:).
I'm trying to create a variable called "b" that includes all trials of "a" where column 24 of "a" contains numbers <2560.
My first solution was
b= a(a(:,24)<2560)
Yet, this solution doesn't render the results I expect.
In order to unpack the problem, I've tried to create a new variable "c" that has only 1 column instead of 24 (like "a" does).
c= [2345; 2780; 345; 2150; 2908; 451]
Now, if I create a variable "d" that includes all rows in "c" that are <2560, the solution I used on the variable "b" above works.
This makes me think the problem lies with the fact that "a" has 24 columns (instead of 1 column, like "c" has). I'm trying to see what I'm missing, but I can't find an answer. Would any of you please be so kind as to help me out?
With gratitude,

  댓글 수: 0

댓글을 달려면 로그인하십시오.

채택된 답변

Birdman 2 Apr 2020
Firstly, at your first line of code, you started checking from the beginning of a. Instead, you should be checking the 23rd column of a
b= a(:,a(:,23)<2560)
Also, you are missing the fact that there is no element in the 23rd column of a that is less than 2560. Therefore that line will return empty.

  댓글 수: 3

Bianca Elena Ivanof
Bianca Elena Ivanof 2 Apr 2020
Thank you for taking the time to help me out.
I've now replaced the former attachment with another variable "a" that contains 24 columns. There are quite a few elements in the 24th column <2560. I've tried out your line of code above:
b= a(:,a(:,24)<2560)
, yet it's saying that "index exceeds matrix dimensions".
Again, I'm trying to deconstruct the problem - this one:
Do you happen to be able to please help me further by any chance?
Ameer Hamza
Ameer Hamza 2 Apr 2020
I guess Birdman mistyped the order of subscripts in his answer. Correct command is
b = a(a(:,24)<2560,:);
Bianca Elena Ivanof
Bianca Elena Ivanof 2 Apr 2020
Perfect. It works now. Thank you both very much.
Stay safe,

댓글을 달려면 로그인하십시오.

추가 답변(0개)


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by