Creating a struct array with a for loop

조회 수: 16 (최근 30일)
Aron Brenner
Aron Brenner 2018년 9월 24일
댓글: Aron Brenner 2018년 9월 24일
If I had three 1x10 arrays, how would I write a for loop that populates a struct array so that the three arrays become a single 3x10 array? For example, if I had three arrays that were {1,3,5,7}, {2,4,6,8}, and {1,2,3,4}, how would I write a for loop that creates a single 3x4 array that looks like {1,3,5,7;2,4,6,8;1,2,3,4}?
  댓글 수: 2
Stephen23
Stephen23 2018년 9월 24일
@Aron Brenner: why are you using cell arrays to hold scalar numeric data? Surely simple numeric arrays would be easier to work with.
Aron Brenner
Aron Brenner 2018년 9월 24일
They aren't numeric data. One of the arrays is populated with strings of text. I just used numeric data for the example.

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

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2018년 9월 24일
a = {1,3,5,7};
b = {2,4,6,8};
c = {1,2,3,4};
out = cat(1,a,b,c)
or
out = [a;b;c]

KSSV
KSSV 2018년 9월 24일
A = {1,3,5,7} ;
B = {2,4,6,8} ;
C = {1,2,3,4} ;
iwant = cell2mat(reshape([A B C],length(A),[])')

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by