How to select specific lines of a table, under a condition ?

3 views (last 30 days)
Hello to everyone,
I have one problem regarding the selection of some specific lines of a table under a condition. I have a matrix(n,3) that contains numbers. What I want to do, is to select only the lines that its both three elements meet the requirement of being under a threshold (named d) and store them in a new matrix(m,3). It is about a solution convergence, so when the program identifies the first line as "true", then all the next lines should be stored in the new matrix, except for the fact that they do not meet the condition d. I have written the next code, but it's not working correctly I think.
for i=1:(size(table,1))
if (table(i,1) < d) && (constable(i,2) < d) && (constable(i,3) < d);
newtable(i,1) = constable(i,1);
newtable(i,2) = constable(i,2);
newtable(i,3) = constable(i,3);
end
end
Could someone please help me and tell me how exactly I have to do this ?
Thank you in advance

Accepted Answer

Sean de Wolski
Sean de Wolski on 23 Mar 2015
Logical indexing and all: ertract all columns (:) where all rows (all,2) are less than 22 (x<22)
x = magic(5)
x(all(x<22,2),:)
Another thing to note is that naming your variable table is a bad idea as this is a MATLAB data type in 13b or newer.
  4 Comments
Sean de Wolski
Sean de Wolski on 23 Mar 2015
Whatever works for you! I was just showing how to build the index based only on the 2nd through 4th column.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!