Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Indexing a 2D matrix into a 3D matrix help

Subject: Indexing a 2D matrix into a 3D matrix help

From: Eric

Date: 11 Nov, 2010 22:39:04

Message: 1 of 5

I would like to index part of a 2-dimensional matrix into specific locations within 3-dimensional matrix. I believe this works using a for loop:

var1 = zeros(4,4,4);
var2 = round(rand(20,4).*10);

r_ind = 1:4;
c_ind = 1:4;
pnt = [1 6 12 18];

for i = 1:4
    var1(r_ind(i),c_ind(i),:) = var2(pnt(i),:);
end

However, if I try vectorizing the code:
var1 = zeros(4,4,4);
var2 = round(rand(20,4).*10);

r_ind = 1:4;
c_ind = 1:4;
pnt = [1 6 12 18];

var1(r_ind,c_ind,:) = var2(pnt,:);

I get the following error:
??? Assignment has fewer non-singleton rhs dimensions than non-singleton
subscripts

Error in ==> test at 17
var1(r_ind,c_ind,:) = var2(pnt,:);

Why does the second piece of code produce an error? Does Matlab do some sort of nested loop when indexing with more than one array?

Any guidance would be greatly appreciated.

Subject: Indexing a 2D matrix into a 3D matrix help

From: Sean

Date: 11 Nov, 2010 22:57:04

Message: 2 of 5

"Eric " <ebaumer@gmail.com> wrote in message <ibhra8$1kt$1@fred.mathworks.com>...
> I would like to index part of a 2-dimensional matrix into specific locations within 3-dimensional matrix. I believe this works using a for loop:
>
> var1 = zeros(4,4,4);
> var2 = round(rand(20,4).*10);
>
> r_ind = 1:4;
> c_ind = 1:4;
> pnt = [1 6 12 18];
>
> for i = 1:4
> var1(r_ind(i),c_ind(i),:) = var2(pnt(i),:);
> end
>
> However, if I try vectorizing the code:
> var1 = zeros(4,4,4);
> var2 = round(rand(20,4).*10);
>
> r_ind = 1:4;
> c_ind = 1:4;
> pnt = [1 6 12 18];
>
> var1(r_ind,c_ind,:) = var2(pnt,:);
>
> I get the following error:
> ??? Assignment has fewer non-singleton rhs dimensions than non-singleton
> subscripts
>
> Error in ==> test at 17
> var1(r_ind,c_ind,:) = var2(pnt,:);
>
> Why does the second piece of code produce an error? Does Matlab do some sort of nested loop when indexing with more than one array?
>
> Any guidance would be greatly appreciated.

doc sub2ind
!

Subject: Indexing a 2D matrix into a 3D matrix help

From: Eric

Date: 11 Nov, 2010 23:26:03

Message: 3 of 5

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ibhsc0$895$1@fred.mathworks.com>...
> "Eric " <ebaumer@gmail.com> wrote in message <ibhra8$1kt$1@fred.mathworks.com>...
> > I would like to index part of a 2-dimensional matrix into specific locations within 3-dimensional matrix. I believe this works using a for loop:
> >
> > var1 = zeros(4,4,4);
> > var2 = round(rand(20,4).*10);
> >
> > r_ind = 1:4;
> > c_ind = 1:4;
> > pnt = [1 6 12 18];
> >
> > for i = 1:4
> > var1(r_ind(i),c_ind(i),:) = var2(pnt(i),:);
> > end
> >
> > However, if I try vectorizing the code:
> > var1 = zeros(4,4,4);
> > var2 = round(rand(20,4).*10);
> >
> > r_ind = 1:4;
> > c_ind = 1:4;
> > pnt = [1 6 12 18];
> >
> > var1(r_ind,c_ind,:) = var2(pnt,:);
> >
> > I get the following error:
> > ??? Assignment has fewer non-singleton rhs dimensions than non-singleton
> > subscripts
> >
> > Error in ==> test at 17
> > var1(r_ind,c_ind,:) = var2(pnt,:);
> >
> > Why does the second piece of code produce an error? Does Matlab do some sort of nested loop when indexing with more than one array?
> >
> > Any guidance would be greatly appreciated.
>
> doc sub2ind
> !

Thanks for the reply. I'm not sure I see how to use linear indexing to pull out a subset array from a matrix and re-assign it to a specific location in another matrix. I guess it's easier to visualize using subscripts.

Also, for pedagogical sake, why is an error message produced for the second piece of code but not the first.

Thanks again for your consideration.

Subject: Indexing a 2D matrix into a 3D matrix help

From: James Tursa

Date: 11 Nov, 2010 23:50:04

Message: 4 of 5

"Eric " <ebaumer@gmail.com> wrote in message <ibhu2b$oue$1@fred.mathworks.com>...
>
> Thanks for the reply. I'm not sure I see how to use linear indexing to pull out a subset array from a matrix and re-assign it to a specific location in another matrix. I guess it's easier to visualize using subscripts.
>
> Also, for pedagogical sake, why is an error message produced for the second piece of code but not the first.
>
> Thanks again for your consideration.

To see what is going on consider this related example:

>> var1 = reshape(1:4*4*4,4,4,4)
var1(:,:,1) =
     1 5 9 13
     2 6 10 14
     3 7 11 15
     4 8 12 16
var1(:,:,2) =
    17 21 25 29
    18 22 26 30
    19 23 27 31
    20 24 28 32
var1(:,:,3) =
    33 37 41 45
    34 38 42 46
    35 39 43 47
    36 40 44 48
var1(:,:,4) =
    49 53 57 61
    50 54 58 62
    51 55 59 63
    52 56 60 64

>> var1([2 3],[2 3],:)
ans(:,:,1) =
     6 10
     7 11
ans(:,:,2) =
    22 26
    23 27
ans(:,:,3) =
    38 42
    39 43
ans(:,:,4) =
    54 58
    55 59

So you were originally trying to just get the nD diagonal elements (for lack of a better term) changed by using a loop and that worked. But your vector indexing didn't work because the syntax you used picks off a sub-matrix like the above example, not scattered elements. So there is a dimension mismatch in the assignment. Hence the need to use something like sub2ind to get at the linear indexes of the nD diagonal elements you want.

James Tursa

Subject: Indexing a 2D matrix into a 3D matrix help

From: Eric

Date: 12 Nov, 2010 02:29:04

Message: 5 of 5

Thanks! The example definitely helps clear things up. I appreciate your time.

Eric

Tags for this Thread

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.

Contact us