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

 Akzeptierte Antwort

Matt J
Matt J am 31 Okt. 2012
Bearbeitet: Matt J am 31 Okt. 2012

0 Stimmen

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 Kommentare

Pamela
Pamela am 31 Okt. 2012
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 am 31 Okt. 2012
Bearbeitet: Matt J am 31 Okt. 2012
Why i had this repetition of values?
Because it's what you asked for. Andrei's solution does the same thing.
Pamela
Pamela am 31 Okt. 2012
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
Matt J
Matt J am 1 Nov. 2012
B{1}=c(A<0);
B{2}=c(A>=0 & A<=7);
B{3}=c(A>7 & A<=10);
Pamela
Pamela am 1 Nov. 2012
thank you

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 31 Okt. 2012

Community Treasure Hunt

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

Start Hunting!

Translated by