How can I correctly use two "for loops" and one "if statement" to insert selected data into a cell array?
Show older comments
I have the following variables: Matrix A with the size 75x1, Matrix B with the size 2400x2, Cell C with the size 75x1.
I am trying to insert B(i,2) into C{j}, if it passes the condition (A(j,1)-0.25)<B(i,1)<(A(j,1)+0.25). I am doing this so that later I can take the averages of each element of C. The code I have written for this is not working the way it should. It puts all the elements of B(:,2) into each cell of C. Any help would be appreciated. Thanks in advance!
C = cell(75,1);
for i = 1:75
for j = 1:2400
if (A(i,1)-0.25) < B(j,1) < (A(i,1)+0.25)
C{i}(end+1)= B(j,2);
end
end
end
D = cell(size(C));
for o = 1:numel(D)
D{o} = mean(C{o});
end
D = cell2mat(D);
Accepted Answer
More Answers (1)
the cyclist
on 20 Jul 2017
A(i,1)-0.25) < B(j,1) < (A(i,1)+0.25
is not going to do what you expect. This needs to be written as two separate conditions that are both checked:
A(i,1)-0.25) < B(j,1) & B(j,1) < (A(i,1)+0.25
Categories
Find more on Desktop in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!