How to create multidimensional array from several vectors

Hello,
I want to increase the performance of a program by vectorizing the code instead of using for-loops. For this I want to create a multidimensional array from several vectors. Below you can find a simplified example using for-loops.
%definition of vectors a,b,c,d
a=1:5;
b=6:10;
c=11:15;
d=16:20;
%creation of multidimensional array M
for u=1:5
M(:,:,u)=[a(u),b(u);c(u),d(u)]
end
I hope you can help me to find a solution for vectorizing this code. Thank you very much in advance!

댓글 수: 1

I think using the function reshape can solve the problem:
%definition of vectors a,b,c,d
a=1:5;
b=6:10;
c=11:15;
d=16:20;
V=[a;b;c;d];
M=reshape(V,[2,2,5])
But maybe there is a better solution. Other ideas are still welcome!

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2018년 2월 6일

댓글:

2018년 2월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by