# Thread Subject: Indexing matrix with dependencies

 Subject: Indexing matrix with dependencies From: Matthew Hyett Date: 19 Jul, 2012 11:11:09 Message: 1 of 4 To the MATLAB community, I'm a novice to the MATLAB language and its many functions and so on, and have had difficulty finding much relevant information on matrix indexing with 'dependencies'. I was wondering if anyone knew of a function call(s) that would effectively solve the following. I have a matrix of values A (for example, 3 columns by 6 rows): A = 1 1 3 1 0 4 1 1 1 1 1 2 2 0 1 2 1 4 I need to print to B a list (of counts or of values) of the third column dependent on the first or the second column, or both. For example, could I get printed, for values of 1 in column 1, the values of the third column dependent on, say, zero in the second? So B looks like: 3 1 2 4 There is probably an easy solution with logical indexing, but I'm more than a little lost in the logic (so to speak). Thanks! Matt
 Subject: Indexing matrix with dependencies From: dpb Date: 19 Jul, 2012 12:51:51 Message: 2 of 4 On 7/19/2012 6:11 AM, Matthew Hyett wrote: > To the MATLAB community, > > I'm a novice to the MATLAB language and its many functions and so on, > and have had difficulty finding much relevant information on matrix > indexing with 'dependencies'. > > I was wondering if anyone knew of a function call(s) that would > effectively solve the following. > > I have a matrix of values A (for example, 3 columns by 6 rows): > > A = > > 1 1 3 > 1 0 4 > 1 1 1 > 1 1 2 > 2 0 1 > 2 1 4 > > I need to print to B a list (of counts or of values) of the third column > dependent on the first or the second column, or both. For example, could > I get printed, for values of 1 in column 1, the values of the third > column dependent on, say, zero in the second? > > So B looks like: > > 3 1 2 4 ... Well, I don't see how B follows from your description at all--the only value where A(:,1)==1 & A(:,2)==0 --> A(:,3)==4 For your result vector A(:,[1:2]) looks like A =       1 1       1 0       1 1       2 1  >> The problem as described --  >> A=[1 1 3;1 0 4; 1 1 1; 1 1 2;2 0 1;2 1 4];  >> B=A(ismember(A(:,[1:2]),[1 0],'rows'),3) B =       4  >> inSet = sum(ismember(A(:,[1:2]),[1 0],'rows')) ans =       1  >> Salt conditions to suit... Look at the doc for doc ismember % and friends for such comparisons/selection processes --
 Subject: Indexing matrix with dependencies From: Steven_Lord Date: 19 Jul, 2012 13:33:15 Message: 3 of 4 "Matthew Hyett" wrote in message news:ju8q0d\$nnn\$1@newscl01ah.mathworks.com... > To the MATLAB community, > > I'm a novice to the MATLAB language and its many functions and so on, and > have had difficulty finding much relevant information on matrix indexing > with 'dependencies'. > > I was wondering if anyone knew of a function call(s) that would > effectively solve the following. > > I have a matrix of values A (for example, 3 columns by 6 rows): > > A = > > 1 1 3 > 1 0 4 > 1 1 1 > 1 1 2 > 2 0 1 > 2 1 4 > > I need to print to B a list (of counts or of values) of the third column > dependent on the first or the second column, or both. For example, could > I get printed, for values of 1 in column 1, the values of the third column > dependent on, say, zero in the second? I think you mean "one in the second", right? Let's build the indexing expression. You want to grab all elements from column 3 of A in rows that meet a certain criterion. B = A(something, 3) That criterion is that the corresponding elements in column 2 of A are equal to 1, if I understand what you're asking. something = A(:, 2) == 1 Read A(:, 2) as the intersection of all rows of A and column 2 of A. Similarly something like A([2 4], [2 3]) would be the elements at the intersections of rows 2 and 4 of A and columns 2 and 3 of A, or in this case [0 4; 1 2]. So your indexing expression is: B = A(A(:, 2) == 1, 3) > So B looks like: > > 3 1 2 4 Since you're extracting from a single column in the indexing expression above, B comes out as a column vector; if you need it to be a row vector, transpose it. -- Steve Lord slord@mathworks.com To contact Technical Support use the Contact Us link on http://www.mathworks.com
 Subject: Indexing matrix with dependencies From: Matthew Hyett Date: 20 Jul, 2012 04:05:10 Message: 4 of 4 "Matthew Hyett" wrote in message ... > To the MATLAB community, > Thanks to you both. Yes, Steven, I meant "one in the second." - apologies (sleepless). I will have a try at these suggestions. Best, Matt

### Everyone's Tags:

Separated by commas
Ex.: root locus, bode

### What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.