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:
Simple matrix operation

Subject: Simple matrix operation

From: Hermano Cappa

Date: 5 Dec, 2011 12:08:08

Message: 1 of 5

Hi,

I'm looking for a simple matrix operation but I can not find the way to go....

A = cat(3,ones(2,2),ones(2,2),ones(2,2));

Subject: Simple matrix operation

From: Hermano Cappa

Date: 5 Dec, 2011 12:16:09

Message: 2 of 5

Sorry, I was a little bit to fast ;-)

Here is my question again:
 
 I'm looking for a simple matrix operation but I can not find the way to go....

 A = cat(3,ones(2,2),ones(2,2),ones(2,2));
 B = [1 2; 3 4; 5 6];

I want to replace each first row of A with the numbers on each row in the vector B. How can I do this without using a loop?

The result must be as follows:

A(:,:,1) =

     1 2
     1 1

A(:,:,2) =

     3 4
     1 1

A(:,:,3) =

     5 6
     1 1

Subject: Simple matrix operation

From: Jan Stolarek

Date: 5 Dec, 2011 12:48:08

Message: 3 of 5

A(1,:,:) = permute(B,[3 2 1])

Subject: Simple matrix operation

From: Bruno Luong

Date: 5 Dec, 2011 12:51:08

Message: 4 of 5

"Hermano Cappa" wrote in message <jbicm9$rtb$1@newscl01ah.mathworks.com>...
> Sorry, I was a little bit to fast ;-)
>
> Here is my question again:
>
> I'm looking for a simple matrix operation but I can not find the way to go....
>
> A = cat(3,ones(2,2),ones(2,2),ones(2,2));
> B = [1 2; 3 4; 5 6];
>
> I want to replace each first row of A with the numbers on each row in the vector B. How can I do this without using a loop?
>
> The result must be as follows:
>
> A(:,:,1) =
>
> 1 2
> 1 1
>
> A(:,:,2) =
>
> 3 4
> 1 1
>
> A(:,:,3) =
>
> 5 6
> 1 1

Try

 A(1,:,:) = permute(B,[3 2 1])

Bruno

Subject: Simple matrix operation

From: Matt J

Date: 5 Dec, 2011 14:40:10

Message: 5 of 5

"Hermano Cappa" wrote in message <jbic78$qgh$1@newscl01ah.mathworks.com>...
> Hi,
>
> I'm looking for a simple matrix operation but I can not find the way to go....
===============

It's a little puzzling why you "can not find the way to go". People have been responding to your question for several days now in several different threads, with very much the same responses which you've been getting here and which avoid the loop just as you require:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/314908#860386

http://www.mathworks.com/matlabcentral/newsreader/view_thread/314938

I will also repeat what I said to you in the first thread above. Your requirement that the operation be fast, and the requirement that it avoid loops may be in conflict. Here is a revised comparison which shows how a for-loop can outrace PERMUTE:


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


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

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

toc;%Elapsed time is 0.300488 seconds.

tic;
for jj=1:M
 A(1,:,:) = permute(B,[3 2 1]);
end
toc%Elapsed time is 0.399419 seconds.

Tags for this Thread

No tags are associated with 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