Add new elements into cell array inside a loop

Hello all!
I I have a set of 10000 values and according to the thresholds I am setting I want to seperate them into individual matrices that would be inside the cell. The matrices elements are distributed trough a loop as above:
for i=1:1:length(TestData)
for k=1:1:4
if TestData(i,3)>=low(k,1) && TestData(i,3)<=high(k,1)
CreatedCell{k}=TestData(i,:);
end
end
end
So how could I store my data into the cell and create multiple matrices? With the way I am doing it now it stores only the last value but I need to store the whole matrices.

3 Comments

Stephen23
Stephen23 on 14 Jan 2019
Edited: Stephen23 on 14 Jan 2019
"With the way I am doing it now it doesnt work"
What does "doesn't work" mean exactly? How are you checking this?
Note that because your indexing into the (badly named) cell array Cell only uses index k you could easily overwrite data on each output loop iteration, in the worst case leaving nothing but the data from the final i loop iteration.
The first issue is that you're using length as a synonym for the number of rows on something that is clearly a matrix. length returns the number of rows or columns, whichever is greater, so you're taking a risk. It would be much better to use size(TestData, 1) to ensure you always get the number of rows.
What does it doesn't work mean? If you get an error, what is the full text of the error? If you don't get the result you expect, then what exactly did you expect and what did you get instead.
Thats exactly what happens. Leaves the data from last iteration, but I need to store all the values. On my code is not named as Cell, just for the understanding here I named it like that.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 14 Jan 2019
Edited: Stephen23 on 14 Jan 2019
This will store the data each iteration of both loops (but I did not change the logical conditions):
N = size(TestData,1); % much better than length
C = cell(N,4);
for ii = 1:N
for jj = 1:4
if TestData(ii,3)>=low(jj,1) && TestData(ii,3)<=high(jj,1)
C{ii,jj} = TestData(ii,:);
end
end
end
Note that this can create copies of the same data in the cell array. This may or may not be what you expect (it is not clear from your question).

4 Comments

I have a set of 10000 values and according to the thresholds I am setting I want to seperate them into individual matrices that would be inside the cell
Stephen23
Stephen23 on 14 Jan 2019
Edited: Stephen23 on 14 Jan 2019
"I have a set of 10000 values and according to the thresholds I am setting I want to seperate them into individual matrices that would be inside the cell"
Do you have a specific question about my answer? You still have not explained how you are checking the code output (which I asked you earlier). It would help if you uploaded some sample input and output data for us to work with (simplified, not the entire 10000 values).
Ok i.e you have a 10000 samples with values from 1 to 100 and you want to seperate them to different matrices according their values :
Matrix1 10-19
Matrix2 20-30
etc
You can use these as inputs to my code:
TestData = ceil(100*rand(10000,1));
low= [10; 20; 40; 60];
high=[20; 40; 60; 80];
Stephen23
Stephen23 on 14 Jan 2019
Edited: Stephen23 on 14 Jan 2019
>> TD = randi([1,100],10000,1); % fake data: random integers [1,100].
>> B = 1:20:101; % bin lower edges.
>> [bin,idx] = histc(TD,B); % match data to bins: B(n)<=data<B(n+1).
>> C = accumarray(idx,TD,[],@(v){v}); % split data according to matched bins.

Sign in to comment.

Categories

Asked:

on 14 Jan 2019

Edited:

on 14 Jan 2019

Community Treasure Hunt

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

Start Hunting!