Assign values to cell array which is preallocated with for loop

3 次查看(过去 30 天)
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!
  1 个评论
Khushboo
Khushboo 2022-11-4
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.

请先登录,再进行评论。

回答(1 个)

Santosh Fatale
Santosh Fatale 2022-11-11
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.
For information about function "find" and "cell", check out following links.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by