Question

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.

Was it helpful?

Solution

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

OTHER TIPS

how about using reshape after [a.f] ? for example:

 matrix =reshape([a.f],numel(a),[]);

How about

matrix = zeros(numel(a),numel(a(1).f)); 
matrix(:) = [a.f];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top