# Thread Subject: Regarding Matrrix Indexing

 Subject: Regarding Matrrix Indexing From: Parag Chandakkar Date: 28 Nov, 2011 17:22:16 Message: 1 of 20 Hi, Suppose I have a matrix "A" of 10x10 of all zeros. Now, I have another matrix say, I=[1 7; 2 1; 3 3; 4 5]. How do I make elements of A one at position indicated by elements by I? That means, matrix "A" should have ones at positions (1,7) , (2,1), (3,3), (4,5). I want to avoid for loops since I have to do this for a bigger matrix. Thanks. - Parag S. Chandakkar.
 Subject: Regarding Matrrix Indexing From: Nasser M. Abbasi Date: 28 Nov, 2011 17:48:18 Message: 2 of 20 On 11/28/2011 11:22 AM, Parag Chandakkar wrote: > Hi, > > Suppose I have a matrix "A" of 10x10 of all zeros. Now, I have another > matrix say, > > I=[1 7; 2 1; 3 3; 4 5]. How do I make elements of A one at position > indicated by elements by I? > That means, matrix "A" should have ones at positions (1,7) , (2,1), > (3,3), (4,5). > I want to avoid for loops since I have to do this for a bigger matrix. > > Thanks. > - Parag S. Chandakkar. may be ---------------- A=zeros(10); siz=size(A); I=[1 7; 2 1; 3 3; 4 5]; A(sub2ind(siz,I(:,1),I(:,2)))=1 ------------------------- A =       0 0 0 0 0 0 1 0 0 0       1 0 0 0 0 0 0 0 0 0       0 0 1 0 0 0 0 0 0 0       0 0 0 0 1 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0 --Nasser
 Subject: Regarding Matrrix Indexing From: Parag S. Chandakkar Date: 28 Nov, 2011 18:05:08 Message: 3 of 20 On Nov 28, 10:48 am, "Nasser M. Abbasi" wrote: > On 11/28/2011 11:22 AM, Parag Chandakkar wrote: > > > Hi, > > > Suppose I have a matrix "A" of 10x10 of all zeros. Now, I have another > > matrix say, > > > I=[1 7; 2 1; 3 3; 4 5]. How do I make elements of A one at position > > indicated by elements by I? > > That means, matrix "A" should have ones at positions (1,7) , (2,1), > > (3,3), (4,5). > > I want to avoid for loops since I have to do this for a bigger matrix. > > > Thanks. > > - Parag S. Chandakkar. > > may be > > ---------------- > A=zeros(10); > siz=size(A); > I=[1 7; 2 1; 3 3; 4 5]; > A(sub2ind(siz,I(:,1),I(:,2)))=1 > ------------------------- > > A = > >       0     0     0     0     0     0     1     0     0     0 >       1     0     0     0     0     0     0     0     0     0 >       0     0     1     0     0     0     0     0     0     0 >       0     0     0     0     1     0     0     0     0     0 >       0     0     0     0     0     0     0     0     0     0 >       0     0     0     0     0     0     0     0     0     0 >       0     0     0     0     0     0     0     0     0     0 >       0     0     0     0     0     0     0     0     0     0 >       0     0     0     0     0     0     0     0     0     0 >       0     0     0     0     0     0     0     0     0     0 > > --Nasser Thank you very much. It works. - Parag S. Chandakkar.
 Subject: Regarding Matrrix Indexing From: dpb Date: 28 Nov, 2011 19:38:48 Message: 4 of 20 On 11/28/2011 12:05 PM, Parag S. Chandakkar wrote: > On Nov 28, 10:48 am, "Nasser M. Abbasi" wrote: >> On 11/28/2011 11:22 AM, Parag Chandakkar wrote: >> >>> Hi, >> >>> Suppose I have a matrix "A" of 10x10 of all zeros. Now, I have another >>> matrix say, >> >>> I=[1 7; 2 1; 3 3; 4 5]. How do I make elements of A one at position >>> indicated by elements by I? ... >> A=zeros(10); >> siz=size(A); >> I=[1 7; 2 1; 3 3; 4 5]; >> A(sub2ind(siz,I(:,1),I(:,2)))=1 >> ------------------------- ... > Thank you very much. It works. ... So would A(I)=1; Didn't you even try it first? If not, it's time for "Getting Started" section on matrices and operations; it's key to using Matlab effectively. --
 Subject: Regarding Matrrix Indexing From: Nasser M. Abbasi Date: 28 Nov, 2011 19:54:20 Message: 5 of 20 On 11/28/2011 1:38 PM, dpb wrote: > On 11/28/2011 12:05 PM, Parag S. Chandakkar wrote: >> On Nov 28, 10:48 am, "Nasser M. Abbasi" wrote: >>> On 11/28/2011 11:22 AM, Parag Chandakkar wrote: >>> >>>> Hi, >>> >>>> Suppose I have a matrix "A" of 10x10 of all zeros. Now, I have another >>>> matrix say, >>> >>>> I=[1 7; 2 1; 3 3; 4 5]. How do I make elements of A one at position >>>> indicated by elements by I? > ... > >>> A=zeros(10); >>> siz=size(A); >>> I=[1 7; 2 1; 3 3; 4 5]; >>> A(sub2ind(siz,I(:,1),I(:,2)))=1 >>> ------------------------- > ... > >> Thank you very much. It works. > ... > > So would > > A(I)=1; > > Didn't you even try it first? > > If not, it's time for "Getting Started" section on matrices and > operations; it's key to using Matlab effectively. > > -- Are you really sure about that? on my system, I get: A=zeros(10); siz=size(A); A(I)=1 A =       1 0 0 0 0 0 0 0 0 0       1 0 0 0 0 0 0 0 0 0       1 0 0 0 0 0 0 0 0 0       1 0 0 0 0 0 0 0 0 0       1 0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0       1 0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0 ? Which is not the same as what the OP wanted, which is A =       0 0 0 0 0 0 1 0 0 0       1 0 0 0 0 0 0 0 0 0       0 0 1 0 0 0 0 0 0 0       0 0 0 0 1 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 0 --Nasser
 Subject: Regarding Matrrix Indexing From: Matt J Date: 28 Nov, 2011 19:55:09 Message: 6 of 20 dpb wrote in message ... > > > Thank you very much. It works. > ... > > So would > > A(I)=1; ========== Don't think so...
 Subject: Regarding Matrrix Indexing From: Roger Stafford Date: 28 Nov, 2011 20:19:08 Message: 7 of 20 dpb wrote in message ... > So would > > A(I)=1; - - - - - - - -   I get what Nasser gets, dpb. My system treats the I elements as linear indices. Roger Stafford
 Subject: Regarding Matrrix Indexing From: dpb Date: 28 Nov, 2011 22:37:53 Message: 8 of 20 On 11/28/2011 2:19 PM, Roger Stafford wrote: > dpb wrote in message ... >> So would >> >> A(I)=1; > - - - - - - - - > I get what Nasser gets, dpb. My system treats the I elements as linear > indices. Yeah, that's "my bad" on that-there one, sorry. It seems like it _should_ work...but doesn't. Don't know precisely why TMW chose the implementation as they did of A(I(:)) instead of the literal meaning of the index array elements. --
 Subject: Regarding Matrrix Indexing From: Steven_Lord Date: 29 Nov, 2011 15:12:33 Message: 9 of 20 "dpb" wrote in message news:jb12fv\$nlj\$1@speranza.aioe.org... > On 11/28/2011 2:19 PM, Roger Stafford wrote: >> dpb wrote in message ... >>> So would >>> >>> A(I)=1; >> - - - - - - - - >> I get what Nasser gets, dpb. My system treats the I elements as linear >> indices. > > Yeah, that's "my bad" on that-there one, sorry. > > It seems like it _should_ work...but doesn't. Don't know precisely why > TMW chose the implementation as they did of A(I(:)) instead of the literal > meaning of the index array elements. I'm not certain either (you'd have to ask Cleve, probably) but I believe it's because linear indexing is useful, and we didn't want A(I) to perform linear indexing UNLESS the matrix I happened to have ndims(A) columns in which case it would switch to subscripted indexing. A = eye(4); A(1:10) = 2; % linear indexing A(1:3) = 3; % linear indexing A(1:2) = 4; % If this just changed A(1, 2), wouldn't that be confusing and break the nice "linear indexing" pattern? A(1) = 5; % linear indexing -- Steve Lord slord@mathworks.com To contact Technical Support use the Contact Us link on http://www.mathworks.com
 Subject: Regarding Matrrix Indexing From: Matt J Date: 29 Nov, 2011 15:35:08 Message: 10 of 20 "Steven_Lord" wrote in message ... > > > It seems like it _should_ work...but doesn't. Don't know precisely why > > TMW chose the implementation as they did of A(I(:)) instead of the literal > > meaning of the index array elements. > > I'm not certain either (you'd have to ask Cleve, probably) but I believe > it's because linear indexing is useful, and we didn't want A(I) to perform > linear indexing UNLESS the matrix I happened to have ndims(A) columns in > which case it would switch to subscripted indexing. ================ The sense in that is pretty clear, although it also seems painful to have to go through SUB2IND all the time, especially since SUB2IND is implemented only in Mcode and is therefore slow. Seems like you could enable an indexing syntax like A({I}) to trigger non-tensorial subscript indexing. Yes, I know. Put in an enhancement request...
 Subject: Regarding Matrrix Indexing From: dpb Date: 29 Nov, 2011 15:53:14 Message: 11 of 20 On 11/29/2011 9:12 AM, Steven_Lord wrote: ... >> It seems like it _should_ work...but doesn't. Don't know precisely why >> TMW chose the implementation as they did of A(I(:)) instead of the >> literal meaning of the index array elements. > > I'm not certain either (you'd have to ask Cleve, probably) but I believe > it's because linear indexing is useful, and we didn't want A(I) to > perform linear indexing UNLESS the matrix I happened to have ndims(A) > columns in which case it would switch to subscripted indexing. > > A = eye(4); > A(1:10) = 2; % linear indexing > A(1:3) = 3; % linear indexing > A(1:2) = 4; % If this just changed A(1, 2), wouldn't that be confusing > and break the nice "linear indexing" pattern? > A(1) = 5; % linear indexing I don't grok the  > A(1:2) = 4; % If this just changed A(1, 2), example comment, exactly. The list as written is explicit and 1D so it would/could be interpreted as linear. If one wanted only A(1,2) then one would write it that way. I think users would have gotten to where it would have been expected behavior if the other parsing had been selected. It would require possibly explicit ':' in places it doesn't as is, I'm not sure; I haven't tried to work on the actual parsing rules in detail. I would guess the choice was strongly owing to the linear nature of matrix storage in memory that was so common to manipulate manually in the days when Cleve was starting to code Matlab. Then we all used to handle "dynamic" memory by things such as large blank COMMON and pass a singly-dimensioned (static) array address around and then use it however the local routine needed before such wonders were handled for us. When so used to the ind2sub<->sub2ind duality manually computed for such "tricks" it would be natural to simply keep that approach. Things were _much_ different way back when... :) (BTW, as shows I'm sure often, being within hailing distance of a just a handful of years of Cleve in longevity means saw/used/wrote a lot of that kind of stuff in days past.) --
 Subject: Regarding Matrrix Indexing From: dpb Date: 1 Dec, 2011 14:25:13 Message: 15 of 20 On 12/1/2011 12:43 AM, Parag Chandakkar wrote: ... > But finally, I would like to tell you that I didn't use Sub2Ind > because that was not providing me the optimized solution. I used > Vectorization to get to the answer. How, specifically??? --
 Subject: Regarding Matrrix Indexing From: Roger Stafford Date: 1 Dec, 2011 16:20:09 Message: 16 of 20 Parag Chandakkar wrote in message ... > But finally, I would like to tell you that I didn't use Sub2Ind > because that was not providing me the optimized solution. I used > Vectorization to get to the answer. > - Parag S Chandakkar - - - - - - - - -   Did you do this:  A(I(:,1)+size(A,1)*(I(:,2)-1)) = 1; If so, that is precisely what 'sub2ind' does. Roger Stafford
 Subject: Regarding Matrrix Indexing From: Matt J Date: 1 Dec, 2011 16:37:08 Message: 17 of 20 "Roger Stafford" wrote in message ... > > Did you do this: > > A(I(:,1)+size(A,1)*(I(:,2)-1)) = 1; > > If so, that is precisely what 'sub2ind' does. ================= Yes, but slower, because of all the input processing it has to do (in Mcode).
 Subject: Regarding Matrrix Indexing From: Parag S. Chandakkar Date: 1 Dec, 2011 18:42:18 Message: 18 of 20 On Dec 1, 9:20 am, "Roger Stafford" wrote: > ParagChandakkar wrote in message ... > > But finally, I would like to tell you that I didn't use Sub2Ind > > because that was not providing me the optimized solution. I used > > Vectorization to get to the answer. > > -ParagSChandakkar > > - - - - - - - - - >   Did you do this: > >  A(I(:,1)+size(A,1)*(I(:,2)-1)) = 1; > > If so, that is precisely what 'sub2ind' does. > > Roger Stafford No, I didn't try... Thanks for telling. - Parag S Chandakkar.
 Subject: Regarding Matrrix Indexing From: Parag S. Chandakkar Date: 1 Dec, 2011 18:41:41 Message: 19 of 20 On Dec 1, 7:25 am, dpb wrote: > On 12/1/2011 12:43 AM,ParagChandakkarwrote: > ... > > > But finally, I would like to tell you that I didn't use Sub2Ind > > because that was not providing me the optimized solution. I used > > Vectorization to get to the answer. > > How, specifically??? > > -- Basically, what I wanted to do was get the locations by sub2Ind at runtime and then assign a particular number only to those locations. But I was using "find" function and then sub2Ind which was making my program slower... So I converted everything into a vector and then by logical indexing I could get those locations, assign a particular number and do this for the entire matrix... then use reshape to get back the matrix... Worked much faster for me... from 20 sec to 3 sec... Thanks. - Parag S Chandakkar.
 Subject: Regarding Matrrix Indexing From: dpb Date: 1 Dec, 2011 19:43:51 Message: 20 of 20 On 12/1/2011 12:41 PM, Parag S. Chandakkar wrote: > On Dec 1, 7:25 am, dpb wrote: >> On 12/1/2011 12:43 AM,ParagChandakkarwrote: >> ... >> >>> ... I used Vectorization to get to the answer. >> >> How, specifically??? >> ... > ... So I converted everything into a vector and then by > logical indexing I could get those locations, assign a particular > number and do this for the entire matrix... then use reshape to get > back the matrix... ... Ok, I grok that. --

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.