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/matrix manipulation question

Subject: vector/matrix manipulation question

From: P

Date: 14 Feb, 2013 20:07:22

Message: 1 of 4

Hello,

I'm trying to figure out the simplest Matlab syntax to deal the following 2 scenarios:

1. let a = [1 2 3; 4 5 6]

what's the cleanest syntax to duplicate each row of a by a factor of x? Let say x = 2, I want to final outcome looking like

b = [1 2 3; 1 2 3; 4 5 6; 4 5 6]

if I use repmat(a, 1, 2), I get
b = [1 2 3; 4 5 6; 1 2 3; 4 5 6]

This is not really what I wanted


2. Is it possible to transpose an inner dimension of a multi-dimension matrx? Let say

[2 3 4] = size(a)

I want to transpose the latter 2 dimension of a such that

[2 4 3] = size(b)

Is there a simple command that can do these?

For both problems, I ended up writing for loop to perform the desired matrix transform. I'm curious if I miss a simpler solution somewhere


Thanks!

Subject: vector/matrix manipulation question

From: Bruno Luong

Date: 14 Feb, 2013 20:15:07

Message: 2 of 4

"P" wrote in message <kfjg5p$mc0$1@newscl01ah.mathworks.com>...
> Hello,
>
> I'm trying to figure out the simplest Matlab syntax to deal the following 2 scenarios:
>
> 1. let a = [1 2 3; 4 5 6]
>
> what's the cleanest syntax to duplicate each row of a by a factor of x? Let say x = 2, I want to final outcome looking like
>
> b = [1 2 3; 1 2 3; 4 5 6; 4 5 6]

See this thread
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326075#896307

>
>
> 2. Is it possible to transpose an inner dimension of a multi-dimension matrx? Let say
>
> [2 3 4] = size(a)
>
> I want to transpose the latter 2 dimension of a such that
>
> [2 4 3] = size(b)

help permute

Bruno

Subject: vector/matrix manipulation question

From: P

Date: 14 Feb, 2013 23:14:12

Message: 3 of 4

Thank you Bruno!

Permute is exactly what I was seeking for.


For 1. your solutions do work for 2D array. I'm wondering if there is solution for 3D array?

For example, let say
a = [1 2 3; 4 5 6];
b = 2*a;
c(1,:,:) = a
c(2,:,:) = b

c(:,:,1) =
     1 4
     2 8
c(:,:,2) =
     2 5
     4 10
c(:,:,3) =
     3 6
     6 12

The desired output e is

e(:,:,1) =
     1 4
     2 8
e(:,:,2) =
     1 4
     2 8
e(:,:,3) =
     2 5
     4 10
e(:,:,4) =
     2 5
     4 10
e(:,:,5) =
     3 6
     6 12
e(:,:,6) =
     3 6
     6 12

Subject: vector/matrix manipulation question

From: Bruno Luong

Date: 14 Feb, 2013 23:37:06

Message: 4 of 4

"P" wrote in message <kfjr44$2v6$1@newscl01ah.mathworks.com>...

>
> For 1. your solutions do work for 2D array. I'm wondering if there is solution for 3D array?
>

Yes. I let you the joy to adapt the code.

Bruno

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