Reputation: 39
My issue is fairly simple. I have vectors of the same size in a struct (let's call it a.f
). Let's say we have 8 structs (8 <1x1 struct>) each containing another structure b which has a vector in it. I'm aware that I can do either this:
for i = 1:8
matrix(i,:) = a(i).f(1:5)
end
or
for i = 1:8
matrix{i} = a(i).f(1:5)
end
Is there another way to store all these arrays into a matrix, without the need for a for loop?
matrix = [a.f];
just stores them all as a vector.
Upvotes: 2
Views: 116
Reputation: 112759
Just use vertcat
if your vectors are rows, or horzcat
if they are columns.
Example:
>> a(1).f = 1:7;
>> a(2).f = 11:17;
>> matrix = vertcat(a.f)
matrix =
1 2 3 4 5 6 7
11 12 13 14 15 16 17
Upvotes: 2
Reputation: 26069
how about using reshape
after [a.f]
? for example:
matrix =reshape([a.f],numel(a),[]);
Upvotes: 2