Thread Subject: Vectorizing Specific Access to Multidimensional Array

Subject: Vectorizing Specific Access to Multidimensional Array

From: First Last

Date: 14 Sep, 2008 06:08:03

Message: 1 of 4

Let's say I have a NxMxL array X where N = M = L = 2:

X(:,:,1) =

1 3
2 4

X(:,:,2) =

5 7
6 8

For each vector along L ([1 5], [3 7], etc.), I want to pick a random element of it (in this case either the first or second element). One 'dumb' way of doing this is:

z = zeros(2,2,2);

for n = 1:N
     for m = 1:M
          v = floor(1 + 2 * rand(1,1));
          z(n,m,v) = 1;
     end
end

The resulting matrix z will end up looking something like, say:

z(:,:,1) =

1 0
0 1

z(:,:,2) =

0 1
1 0

and X .* z is

(:,:,1) =

1 0
0 4

(:,:,2) =

0 7
6 0

so that sum(X .* z,3) =

1 7
6 4

Is there any way to create a random matrix z directly so that I can avoid those loops?

Better yet, is there a way to read a random matrix R that has the elements

R = floor(1 + 2 * rand(2,2));

which might look like

1 2
2 1

that I could use to directly access elements of X, using R along the dimension L?

I realize this may be very confusing. Of course I'm happy to clarify anything I can. Thanks.

Subject: Vectorizing Specific Access to Multidimensional Array

From: Bruno Luong

Date: 14 Sep, 2008 06:48:02

Message: 2 of 4

"First Last" <nospam@nospamplease.com> wrote in message <gai9o3$fer$1@fred.mathworks.com>...

Not sure if this is what you want. The function that you need is "sub2ind".

% Data
M=3; N=4; L=5;
X=ceil(10*rand(M,N,L));

[i j]=ndgrid(1:M,1:N);
% random pick of index from to L
k = floor(1 + L * rand(size(i)));
k(k>L) = L; % clip incase rand returns "1"

linind = sub2ind(size(X),i(:),j(:),k(:));

% Generate z
z=zeros(size(X));
z(linind)=1;
sum(X.*z,3)

% Access X directly
Y = X(reshape(linind,size(i)))

% Bruno

Subject: Vectorizing Specific Access to Multidimensional Array

From: Bruno Luong

Date: 14 Sep, 2008 07:09:02

Message: 3 of 4

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gaic32$st9$1@fred.mathworks.com>...

> k = floor(1 + L * rand(size(i)));
> k(k>L) = L; % clip in case RAND returns "1"
>

Note: the clipping statement might not be necessary, as rand never return 1. The floor(L*(1-eps)) always returns L-1 is something I wasn't sure, but this might be right. Someone can confirm?

Bruno

Subject: Vectorizing Specific Access to Multidimensional Array

From: First Last

Date: 14 Sep, 2008 07:46:02

Message: 4 of 4

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gaic32$st9$1@fred.mathworks.com>...
> "First Last" <nospam@nospamplease.com> wrote in message <gai9o3$fer$1@fred.mathworks.com>...
>
> Not sure if this is what you want. The function that you need is "sub2ind".
>
> % Data
> M=3; N=4; L=5;
> X=ceil(10*rand(M,N,L));
>
> [i j]=ndgrid(1:M,1:N);
> % random pick of index from to L
> k = floor(1 + L * rand(size(i)));
> k(k>L) = L; % clip incase rand returns "1"
>
> linind = sub2ind(size(X),i(:),j(:),k(:));
>
> % Generate z
> z=zeros(size(X));
> z(linind)=1;
> sum(X.*z,3)
>
> % Access X directly
> Y = X(reshape(linind,size(i)))
>
> % Bruno

This solution is perfect. Thanks very much. You have saved my department's electricity bill and my sanity!

Tags for this Thread

Add a New Tag:

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com