how to give a name to every case in the loop as the following example ?
Info
This question is closed. Reopen it to edit or answer.
Show older comments
i have a loop which looks like
clc
clear
a=[1:10];
for k=1:10;
b=nchoosek(a,k)
end
but, i want to give a name to every case in the loop in order to be as well: b1=.... b2=.... b3=.... . . . b10=.... until i can call up any one of them separately. i have tried to use this code '(eval(sprintf('b%d = [1:i]',i))' but it ended in failure . please , help me thank you
Answers (1)
Azzi Abdelmalek
on 14 Jun 2014
Edited: Azzi Abdelmalek
on 14 Jun 2014
a=[1:10];
for k=1:10;
b{k}=nchoosek(a,k)
end
It's not good to create several variables, just use a cell class, or or a struct class
4 Comments
mohammad
on 14 Jun 2014
Azzi Abdelmalek
on 14 Jun 2014
Edited: Azzi Abdelmalek
on 14 Jun 2014
If you insist to create those variables
a=[1:10];
for k=1:10;
eval(sprintf('b%d=nchoosek(a,%d)',k,k))
end
But instead of creating b1,b2,... it's better if you cretae one cell array (look at my previous answer), if you want to access to, for example the third array just write
b{3}
mohammad
on 14 Jun 2014
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!