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:
Vector into 3D matrix

Subject: Vector into 3D matrix

From: Hermano Cappa

Date: 2 Dec, 2011 15:38:07

Message: 1 of 5

Hi,

I have a 3D matrix: A = zeros(3,3,3);

I have a vector: B = [1 5 8];

Now, I want to put the vector B on each first row of matrix A

I thought this can easily be done with: A(1,:,:) = B; but I get the following message:

??? Assignment has fewer non-singleton rhs dimensions than non-singleton
subscripts

Question: How can I do this very fast without using a loop?
 

Subject: Vector into 3D matrix

From: Ram-Li-Czech

Date: 2 Dec, 2011 15:53:08

Message: 2 of 5

A(1,:,:)=repmat(B,3,1);

Ram-Li-Czech

"Hermano Cappa" wrote in message <jbarcv$qqk$1@newscl01ah.mathworks.com>...
> Hi,
>
> I have a 3D matrix: A = zeros(3,3,3);
>
> I have a vector: B = [1 5 8];
>
> Now, I want to put the vector B on each first row of matrix A
>
> I thought this can easily be done with: A(1,:,:) = B; but I get the following message:
>
> ??? Assignment has fewer non-singleton rhs dimensions than non-singleton
> subscripts
>
> Question: How can I do this very fast without using a loop?
>

Subject: Vector into 3D matrix

From: Hermano Cappa

Date: 2 Dec, 2011 16:06:08

Message: 3 of 5

Hi Ram-Li-Czech,

This gives not the expected response namely

1 1 1
0 0 0
0 0 0

5 5 5
0 0 0
0 0 0

8 8 8
0 0 0
0 0 0

instead of:

1 5 8
0 0 0
0 0 0

1 5 8
0 0 0
0 0 0

1 5 8
0 0 0
0 0 0



"Ram-Li-Czech" wrote in message <jbas94$vd$1@newscl01ah.mathworks.com>...
> A(1,:,:)=repmat(B,3,1);
>
> Ram-Li-Czech
>
> "Hermano Cappa" wrote in message <jbarcv$qqk$1@newscl01ah.mathworks.com>...
> > Hi,
> >
> > I have a 3D matrix: A = zeros(3,3,3);
> >
> > I have a vector: B = [1 5 8];
> >
> > Now, I want to put the vector B on each first row of matrix A
> >
> > I thought this can easily be done with: A(1,:,:) = B; but I get the following message:
> >
> > ??? Assignment has fewer non-singleton rhs dimensions than non-singleton
> > subscripts
> >
> > Question: How can I do this very fast without using a loop?
> >

Subject: Vector into 3D matrix

From: ade77

Date: 2 Dec, 2011 17:21:07

Message: 4 of 5

One of many options is this:

A(1,:,:) = repmat(B, [1 1 3]).

To generalize it:
A(1,:,:) = repmat(B, [1, 1, size(A,3)]);

Although this method might not be memory efficient, other options might be available to handle memory better.

Subject: Vector into 3D matrix

From: Matt J

Date: 2 Dec, 2011 22:30:09

Message: 5 of 5

"Hermano Cappa" wrote in message <jbarcv$qqk$1@newscl01ah.mathworks.com>...
>
>
> Question: How can I do this very fast without using a loop?
==================

But a loop may very well be the fastest. Compare:


M=10;%repetitions
N=300;
A=rand(N,N,N);
B=rand(1,N);


tic
for jj=1:M
    for ii=1:N

       A(1,:,ii)=B;
    end
end

toc;%Elapsed time is 0.019716 seconds.

tic;
for jj=1:M
 A(1,:,:) = repmat(B, [1, 1, N]);
end
toc%Elapsed time is 0.043378 seconds.

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