0

I have the following array of struct:

item.Position=[];
item.Cost=0;
items=repmat(item,1000,1);
for i=1:1000
    items(i).Position = floor(ones(1,5)*rand*10);
    items(i).Cost = rand;
end

I want to extract the Position as 2D array. So, the result should be:

1 2 3 4 5 6 7
9 2 4 1 0 3 4 
5 4 3 2 4 9 8
....
0 2 4 8 6 3 1 

is it possible without looping in MATLAB?

0

1 Answer 1

3

You can collect the output of items.Position using []. This will, however, collect it as one long array. So to obtain the matrix you will have to reshape it, i.e. something like

%Build data
item.Position=[ 1 2 3 4 5 6 7];
item.Cost=0;
items=repmat(item,1000,1);

%Collect output
tmp = [items.Position];

%Reshape
res = reshape(tmp,7,[]).';

The collection and reshape can of course be done in 1 step, I just divided it up for clarity.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.