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

how to creating new matrix which is the variables come from 2 different matrix..?

Asked by Noru on 16 Apr 2013

I have a little matrix problem which perhaps interesting for you. Here it comes :

I have 2 matrix like below :

A = 8     1
    9     3
    1     5
    9    10
    6    10
B = 0.1576    0.1419
    0.9706    0.4218
    0.9572    0.9157
    0.4854    0.7922
    0.8003    0.9595

And i want to join those two matrix and separate each line of those two matrix above become 5 different matrix with loop process, like below :

C(1) = 8   0.1576
       1   0.1419
C(2) = 9   0.9706
       3   0.4218
C(3) = 1   0.9572
       5   0.9157
C(4) = 9   0.4854
       10  0.7922
C(5) = 6   0.8003
       10  0.9595

I am a rookie in matlab and still need an assist for some matlab problem from every pro in this forum.

Anyone have the solution..?

thank you..

0 Comments

Noru

Products

No products are associated with this question.

2 Answers

Answer by Andrei Bobrov on 16 Apr 2013
Edited by Andrei Bobrov on 16 Apr 2013
Accepted answer
C = permute(cat(3,A,B),[2 3 1])

in cell array

Ccell = reshape(num2cell(C,[1 2]),[],1);

with for-loop

C2 = cell(size(A,1),1)
for jj = 1:numel(C2)
    C2{jj} = [A(jj,:);B(jj,:)]';
end

3 Comments

Noru on 18 Apr 2013

when i using your loop operation why it just created matrix like below :

C2 = 
    [2x2 double]
    [2x2 double]
    [2x2 double]
    [2x2 double]
    [2x2 double]

Am I doing mistake..?

Andrei Bobrov on 18 Apr 2013

No, try below:

C2{:}

or

C2{1}

and please read about cell array.

Noru on 18 Apr 2013

ok thank you for your help.. it really works..

Andrei Bobrov
Answer by Matt J on 16 Apr 2013

You could have your 2x2 matrices as sheets C(:,:,i) of a 3D array, just by doing

 C=reshape([A,B].',2,2,[])

1 Comment

Noru on 18 Apr 2013

thank's for your solution too..

Matt J

Contact us