0

say I have got two cell arrays a, b:

for k=1:3
    a{k} = nan(3, k);
end
b = {ones(1, 1), ones(1, 2), ones(1, 3)};

how I assign each cell in b into the second line of each cell of a?

2
  • I truly appreciate your effort but it did not work for me. I have upvoted your comment... Commented May 10, 2020 at 21:05
  • Can you clarify what you mean by it did not work? Commented May 10, 2020 at 21:13

1 Answer 1

1

Just have a loop:

for i=1:size(a,2)
    a{i}(2,:) = b{i}
end

If a and b are small, you can use deal:

[a{1}(2,:) a{2}(2,:) a{3}(2,:)] = deal(b{:});
Sign up to request clarification or add additional context in comments.

6 Comments

this is answering my question but considering that I am asking a minimal question, would it solve a case where k=1:50?
Although I honestly think you should just do a simple loop
I dont feel good with the eval solution but you are probably right about the loop solution.
I just wanted to show that that is one option. I personally don't see an alternative to this or the loop. I don't think you are going to find a different "magic" oneliner. Assigning stuff to comma separated outputs in this scenario is tricky.
I'd be very cautious with suggesting eval, especially without any warnings. See this answer of mine and references therein on why it's best to avoid it whenever possible. Gist is: hard to debug errors and slow execution due to disabling of the JIT.
|

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.