How to place contents into a specif cell index?

1 view (last 30 days)
Hello, I'm having a syntex issue with cellfun (I think).
I have the following:
A = cell(1,2);
B = cell(1,2);
A(1,1) = {rand(3,4,5)};
B(1,1) = {rand(3,4)};
A(1,2) = {rand(3,4,5)};
B(1,2) = {rand(3,4)};
I'd like to go over each cell in A, and place there the corresponding cell B, at location A(:,:,3).
I'm guessing cellfun should be used, but I can't figure it out how to write correctly the syntax.
Help please, someone?
  1 Comment
Adam on 13 Jun 2017
So you want to create a 3d cell array as the result?
cat( 3, A, B )
ought to do what you want if I understand correctly, though A(:,:,3) is a bit confusing.
You cannot use cellfun to resize an array - it will just operate over the elements in an array and give an output of the same size.

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 13 Jun 2017
Edited: Andrei Bobrov on 13 Jun 2017
AA = cat(4,A{:});
AA(:,:,3,:) = cat(3,B{:});
A = squeeze(num2cell(AA,1:3));
or with loop for..end
for ii = 1:numel(A)
A{ii}(:,:,3) = B{ii};

More Answers (0)


Find more on Creating and Concatenating Matrices in Help Center and File Exchange


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!