3

I have a similar question to my previous one. This time the form of the nested structure looks like this:

Sizes = [2, 5, 8, 6, 3];
cells = 5;
for i = 1:cells
    for j = 1:Sizes(i)
        a(i).b.c(j).d = rand(1,1);
    end
    a(i).b.Size = Sizes(i);
end

Again I would like to put all the d values of a(:).b.c(:) into a single cell array that contains 1 x cells cells.

Here is my solution using cellfun but I would like to avoid this function:

ab = [a.b];
abc = {ab.c};
abcd = cellfun(@(x) [x.d], abc, 'UniformOutput', false);

Using the previous solution for abc:

abc = [ab.c];

creates a 1x24 struct array with field d. I thought of using the Size field to reshape this result into a cell array but I don't know how or if it is possible. Do you have a better appraoch without using loops and without cellfun?

1 Answer 1

2

You can do this using mat2cell as follows:

ab = [a.b];
abc = [ab.c];
abcd = mat2cell([abc.d], 1, [ab.Size]);
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.