How can i concatenate array of 3 to form single matrix?

조회 수: 1 (최근 30일)
Aliff Zin
Aliff Zin 2017년 1월 29일
답변: Andrei Bobrov 2017년 1월 29일
i am very new to matlab, so sorry if i sounded stupid
i just need to get an output of 3 array from my looping and form one single 3d matrix
for example
100 array x 10 array x 12 array and store it as 3d matrix
so that the size will be : 100 x 10 x 12
any idea?
and thank you in advanced
  댓글 수: 1
Jan
Jan 2017년 1월 29일
An important detail is missing: I assume "100 array" means a vector of the size [1 x 100] or [100 x 1]. Then combining these vectors need to apply a mathematical operation, multiplication or addition etc. Please post some example input data and the wanted output, e.g. with the sizes 2,3,4.

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

채택된 답변

Jan
Jan 2017년 1월 29일
편집: Jan 2017년 1월 29일
A bold guess:
x = 1:100;
y = 1:10;
z = 1:12;
Result = reshape(x, [], 1, 1) + reshape(y, 1, [], 1) + reshape(z, 1, 1, []);
Here I assumes, that you want to combine the values by an addition.
This runs in R2016b. The reshape commands are formulated equivalently for demonstration purposes only: In the first you can omit the trailing 1, the second is not needed because y has the wanted shape already.
For earlier version, the addition of arrays with different shapes required bsxfun:
Result = bsxfun(@plus, reshape(x, [], 1, 1), ...
bsxfun(@plus, reshape(y, 1, [], 1), reshape(z, 1, 1, [])));
  댓글 수: 1
Aliff Zin
Aliff Zin 2017년 1월 29일
Guess not all superheroes wear capes, thank you @Jan Simon !

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 1월 29일
[xx,yy,zz] = ndgrid(x,y,z);
res = xx+yy+zz

카테고리

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