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

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

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

Thank you very much. If I would like to have the date/time of the data as 1st column and the aforementioned numeric data in 2nd, 3rd and 4th columns, how could I do the same thing? I suppose that I shouldn't use the (all,2). Can I just apply this to columns 2nd, 3rd and 4th, and store them in new matrix but with their date/time ?
Actually, I did not use "table" as name of my matrix, I just wrote this here so as everyone to get it. But thank you for your note and your time.
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)

Categories

Community Treasure Hunt

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

Start Hunting!