How to create this matrix ?

조회 수: 1 (최근 30일)
Akash Pal
Akash Pal 2022년 7월 10일
편집: John D'Errico 2022년 7월 10일
I have a matrix whose size is 2x5x2 double and i have another matrix which is 2X5 double ,i just want to add together and make a simple matrix A,
in this A matrix there will be 2 rows ,total 15 column .
  댓글 수: 2
Sam Chak
Sam Chak 2022년 7월 10일
Can you really add matrices of incompatible sizes?
Akash Pal
Akash Pal 2022년 7월 10일
yes i know its impossible but if there is any solution .

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2022년 7월 10일
편집: John D'Errico 2022년 7월 10일
It appears the request was to concatenate the two planes of A into one plane, and then concatenate another matrix to the columns of the result. So it would appear the word add was used in a suggestively wrong way. And since concatenation is the only way one could reasonably manipulate a 2x5x2 array, with another 2x5 array, to finally create a 2x15 result, that must surely be the case.
A = randi(5,[2,5,2])
A =
A(:,:,1) = 2 5 5 1 5 2 4 5 5 2 A(:,:,2) = 2 5 3 5 2 2 5 4 1 5
B = randi([6,10],[2,5])
B = 2×5
9 10 7 9 6 7 8 7 7 6
So we have two matrices of the desires sizes.
Concatenation is easy. We could either first reshape A using the reshape function, or just extract the two planes of A. The latter is easier.
C = [A(:,:,1),A(:,:,2),B]
C = 2×15
2 5 5 1 5 2 5 3 5 2 9 10 7 9 6 2 4 5 5 2 2 5 4 1 5 7 8 7 7 6
However, reshape would have also worked, if we then used concatenation.
D = [reshape(A,[2,10]),B]
D = 2×15
2 5 5 1 5 2 5 3 5 2 9 10 7 9 6 2 4 5 5 2 2 5 4 1 5 7 8 7 7 6
As you can see, both operations worked nicely enough. If the matrix A had many planes, then reshape would be the better option.
Finally, it is crucially important to understand how the elements of a matrix are stored internally before/when you use reshape.

추가 답변 (1개)

Sam Chak
Sam Chak 2022년 7월 10일
Despite the Matrix Law forbids the operation, we can nearly always perform miracles with MATLAB, so long as you give the example of the desired output. Given M and N, how do you want to display the output of
M(:,:,1) = [1 2 3 4 5; 6 7 8 9 10];
M(:,:,2) = 2*[1 2 3 4 5; 6 7 8 9 10];
M
M =
M(:,:,1) = 1 2 3 4 5 6 7 8 9 10 M(:,:,2) = 2 4 6 8 10 12 14 16 18 20
N = 3*M(:,:,1)
N = 2×5
3 6 9 12 15 18 21 24 27 30

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by