0

I have a for loop and each value a{i} b{i} c{i} is equal each time with a specific number. So I was wondering how can I put all those value in an array through loop. The way that I am using I mean this one [a{i};b{i};c{i}] it seems that it doesn't work! If I keep 2 out of three values is working but I want the data from all of the values (a b c)

You can see the (pseudo)code below:

for i=1:number of cells
   Cell{i}.Tri=[a{i};b{i};c{i}]
end
1
  • How does it not work? What is the data a b and c? Since you said without the third value it is working please show us a minimal reproducible example Commented Mar 19, 2017 at 19:07

2 Answers 2

1

cell2mat is what you need:

a = num2cell(rand(1,10));
b = num2cell(rand(1,10));
c = num2cell(rand(1,10));
abc = cell2mat([a;b;c]);
Sign up to request clarification or add additional context in comments.

Comments

0

This can be done without a for loop by using cellfun combined with the cat function. EDIT: As noted in the comments, cellfun is itself a loop.

% Create all variables
a{1}=rand(10);
a=repmat(a,10,1);
b=a;
c=a; 
% Add a cell array of equal size to a. The contents of each cell are the dimension along which to concatenate. 
catarg=num2cell(ones(size(a)))

% Do the concatenation
d=cellfun(@cat,catarg,a,b,c,'UniformOutput',false);

1 Comment

cellfun is a loop.

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.