# Assign values to cell array which is preallocated with for loop

7 views (last 30 days)
Dennis on 2 Nov 2022
Answered: Santosh Fatale on 11 Nov 2022
numEleIndexVector = length(freqIntervCntr); %11
numEleFreqFiltered = length(frequencyFiltered); %619
indexCell = cell(1,numEleIndexVector);
indexCell(1:numEleIndexVector) = {NaN(1,numEleFreqFiltered)};
%divider_freq = 0.05
for i = 0:(numEleIndexVector-1)
for j = 1:numEleFreqFiltered
if i == 0
indexCell{1,1+i} = find(frequencyFiltered(j) <= divider_freq);
else
indexCell{1,i+1} = find((divider_freq*(i)) < frequencyFiltered(j) & frequencyFiltered(j) <= (divider_freq*(i+1)));
end
end
end
Hey guys, I want to to find the indices which are meeting the defined conditions.
Also I would like to delete all of the NaN values left in the cell afterwards.
Grateful for any help!
Khushboo on 4 Nov 2022
Hello,
Could you please give more details regarding what are the array values of frequencyFiltered? It would also be helpful if you could give an idea what exactly are you trying to do and the expected output.

Santosh Fatale on 11 Nov 2022
Hi Dennis,
I investigated the code provided by you, and below is the modified code that I think will achieve the desired output.
frequencyFiltered = randi(100, 619, 1);
numEleIndexVector = 11
numEleFreqFiltered = length(frequencyFiltered); %619
indexCell = cell(1, numEleIndexVector);
% indexCell(1:numEleIndexVector) = {NaN(1,numEleFreqFiltered)};
divider_freq = 2
for i = 0:(numEleIndexVector-1)
% Note that I removed the extra for loop here.
if i == 0
indexCell{1,1+i} = find(frequencyFiltered <= divider_freq);
else
indexCell{1,i+1} = find((divider_freq*(i)) < frequencyFiltered & frequencyFiltered <= (divider_freq*(i+1)));
end
end
You do not need internal loop with variable 'j', which was a part of the earlier code.