way to pull information from matrix - if X then store Y, but only if Y is not the last number in the column?

Hi,
I'm looking to pull information from a matrix with an if statement. But when the if statement is true, only to pull the information if it is not the last row in the matrix. Is there a way to do this?
So for example, if column one is made up of 1's and 0's, I want it to store the information from the corresponding row's column two only when column one shows 1's, and only when it is not the last entry in the matrix.
Thanks in advance to any help offered.
Mamie

 Accepted Answer

ix=x(1:end-1,1)==1; % all except last if 1
z=x(ix,2); % pick those up
Can, of course, eliminate the intermediate variable; just wrote as separate step to make easier to read without the additional level of indexing--
z=x(x(1:end-1,1)==1,2);
Moral: if the last row can't ever contribute to the result, throw it away from the start...

More Answers (0)

Asked:

on 19 Aug 2015

Commented:

on 19 Aug 2015

Community Treasure Hunt

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

Start Hunting!