How can I fill a cell array?

Hi,
To create a cell array with 3 cells should I do this?
B = cell(3,2);
To fill this cell array with values from c according to the values in A knowing that c and A are a vectors. should I do this?
number=length(A);
for i=1:number
if (A<0)
B{1} = [B{1} c];
elseif (0<=A<=7)
B{2} = [B{2} c];
elseif (7<A<=10)
B{3} = [B{3} c];
end
end
thanks

 采纳的回答

Matt J
Matt J 2012-10-31
编辑:Matt J 2012-10-31
Here's a different approach,
B=cell(3,1);
f=@(m) repmat(c,1,nnz(m));
B{1}=f(A<0);
B{2}=f(A>=0 & A<=7);
B{3}=f(A>7 & A<=10);

5 个评论

Thak you Sir
Can you help me to correct this code to fill this cell array B with values from c according to the values in A.
For example the first value in A is 7<9<=10. Then the first value in c should be stored in B{3}
A = [9 3 -2 8 -4 10]
B = cell(3,1);
c =[22.398 13.251 25.982 11.369 47.324 6.6971];
f=@(m) repmat(c,1,nnz(m));
B{1}=f(A<0)
B{2}=f(A>=0 & A<=7)
B{3}=f(A>7 & A<=10)
To see the values in B I used these lines
o=B{1}
m=B{2}
l=B{3}
I had
o =
22.3980 13.2510 25.9820 11.3690 47.3240 6.6971 22.3980 13.2510 25.9820 11.3690 47.3240 6.6971
m =
22.3980 13.2510 25.9820 11.3690 47.3240 6.6971
l =
Columns 1 through 12
22.3980 13.2510 25.9820 11.3690 47.3240 6.6971 22.3980 13.2510 25.9820 11.3690 47.3240 6.6971
Columns 13 through 18
22.3980 13.2510 25.9820 11.3690 47.3240 6.6971
Why i had this repetition of values?
Matt J
Matt J 2012-10-31
编辑:Matt J 2012-10-31
Why i had this repetition of values?
Because it's what you asked for. Andrei's solution does the same thing.
How can I modify the code to have a result like this knowing that A and c have the same size
o =
25.9820 47.3240
m =
13.2510
l =
22.3980 11.3690 6.6971
For example the first value in A is 9. It is between 7 and 10. Then the first value in c (22.3980) should be stored in B{3}. The second value in A is 3. It is between 0 and 7. Then the second value in c (13.251) should be stored in B{2}. thanks
B{1}=c(A<0);
B{2}=c(A>=0 & A<=7);
B{3}=c(A>7 & A<=10);
thank you

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Operators and Elementary Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by