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:
Expand Matrix

Subject: Expand Matrix

From: Barry

Date: 30 Aug, 2010 00:00:21

Message: 1 of 10

I have a matrix A 2x2 I want to extend it to a 4x2 by dividing and distributing it over 2 rows

A
2 4
8 10


B
1 2
1 2
4 5
4 5

Subject: Expand Matrix

From: ImageAnalyst

Date: 30 Aug, 2010 00:21:20

Message: 2 of 10

OK. I assume there's an implied question in this....???
Have you tried even a simple for loop, where you get a row from A,
divide it by 2 and concatenate it onto B? Should be easy, even for a
beginner.

Subject: Expand Matrix

From: Barry

Date: 30 Aug, 2010 00:29:05

Message: 3 of 10

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <e4e36ddd-e759-4d47-a679-02936055081f@m1g2000yqo.googlegroups.com>...
> OK. I assume there's an implied question in this....???
> Have you tried even a simple for loop, where you get a row from A,
> divide it by 2 and concatenate it onto B? Should be easy, even for a
> beginner.

Ya i figured it out:

C=ones(size(A));
C(1,size(A))=(A);
C(2,1:size(A))=(A);
D=reshape(C,4,2);

Subject: Expand Matrix

From: ImageAnalyst

Date: 30 Aug, 2010 01:12:48

Message: 4 of 10

Ya? No you didn't. That doesn't even run, much less give you the
desired answer.

Well since you at least gave it a try, here is one solution (that DOES
work):

A=[2 4; 8 10]
numberOfRowsInA = size(A, 1);
B = ones(numberOfRowsInA, 2);
rowB = 1;
for row = 1:numberOfRowsInA
    thisRowOfA = A(row, :);
    newRowForB = thisRowOfA/2;
    B(rowB, :) = newRowForB;
    B(rowB+1, :) = newRowForB;
    rowB = rowB + 2;
end
% Display B
B

Subject: Expand Matrix

From: James Tursa

Date: 30 Aug, 2010 01:49:04

Message: 5 of 10

"Barry " <barryoconnell2@gmail.com> wrote in message <i5esal$1c5$1@fred.mathworks.com>...
> I have a matrix A 2x2 I want to extend it to a 4x2 by dividing and distributing it over 2 rows
>
> A
> 2 4
> 8 10
>
>
> B
> 1 2
> 1 2
> 4 5
> 4 5

Another of the many solutions:

 clear B
 A =[2 4;8 10];
 z = size(A);
 A2 = A / 2;
 B(2:2:2*z(1),:) = A2;
 B(1:2:2*z(1)-1,:) = A2;


James Tursa

Subject: Expand Matrix

From: Matt J

Date: 30 Aug, 2010 02:41:04

Message: 6 of 10

Another solution


B=kron(A,[1;1])

Subject: Expand Matrix

From: ImageAnalyst

Date: 30 Aug, 2010 02:59:11

Message: 7 of 10

On Aug 29, 10:41 pm, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> Another solution
>
> B=kron(A,[1;1])
--------------------------------
Huh?
Your solution:
A=[2 4;8 10]
B=kron(A,[1;1])
B =

     2 4
     2 4
     8 10
     8 10
What he wanted was:
B
1 2
1 2
4 5
4 5
Even if you multiply by two it doesn't do what he wants, to divide a
single row by two and replace it with two rows each with the half
values (it does in this one case, but not in general)

Subject: Expand Matrix

From: Matt J

Date: 30 Aug, 2010 03:11:03

Message: 8 of 10

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <b808ac9a-a0fc-4cb6-8b7a-62f132ed47df@f42g2000yqn.googlegroups.com>...

> What he wanted was:
> B
> 1 2
> 1 2
> 4 5
> 4 5

OK, I didn't understand the OP, but this should fix it

>> B=kron(A,[1;1]/2)

B =

     1 2
     1 2
     4 5
     4 5



> Even if you multiply by two it doesn't do what he wants, to divide a
> single row by two and replace it with two rows each with the half
> values (it does in this one case, but not in general)

Lost you. What would I be multiplying by two and why? I've _divided_ one of the kron args by two, but that should do exactly what you've described, in all cases.

Subject: Expand Matrix

From: ImageAnalyst

Date: 30 Aug, 2010 03:27:50

Message: 9 of 10

Yes, I meant divide by 2. But anyway, your new solution does seem to
do what he wants in a compact one line form. I never knew that kron
function existed.

Subject: Expand Matrix

From: Matt J

Date: 30 Aug, 2010 03:42:05

Message: 10 of 10

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <d5426b18-36d2-4332-8232-ba6e06cddebf@i31g2000yqm.googlegroups.com>...
> Yes, I meant divide by 2. But anyway, your new solution does seem to
> do what he wants in a compact one line form. I never knew that kron
> function existed.
=========

No? Well, for most things, I don't really recommend it. kron() often tends to produce very large, memory-consuming matrices. Instead, I prefer this,

http://www.mathworks.com/matlabcentral/fileexchange/25969-efficient-object-oriented-kronecker-product-manipulation

Don't know if this would suit the OP, however...

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