saving values without index

%***question updated***%
I have the following situation:-
for d=1:numel(Time_delay) % time delay is (8x1000) matrix
if rem(d,8)==0;
mic_out(1,:)=mic_in(1,:);
out(rem(d,8)+1,:)=sum(mic_out,1);
y=sum(out.^2,2);
mic_out=zeros(size(mic_out));
end
mic_out(rem(d,8)+1,:)= del(mic_in(rem(d,8)+1,:),fs,Time_delay(d+1),2000); %here 'del' is a function which delays the 8 signals in 'mic_in(8x10000)' according to delays specified in Time_delay'
end
The first row in Time_delay is all zeros. So, there is no delay applied. Hence, I replace it with 1st row of signal that is, mic_in(1,10000). Each time I change column in Time_delay, I need to add the 8 delayed signals in mic_out row-wise,square it and add them colum wise(i.e. calculate energy) and store only the energy value and move on.
Then repeat the above process for all columns of Time_delay.
At the end I need only 'y' matrix containing the energies.
Whats the most efficient way to achive this? Please help

回答(2 个)

Why do you not want to use an index? It is less efficient to just append values to an existing array, but you can:
x = [];
for k = 1:5
x = [x,k^2];
end
or even x(end+1) = ...
If the values are of different types or dimensions, use a cell array -- e.g.:
x = {};
for k = ...
x{end+1} = ... % or x = [x,...];
end

4 个评论

can we initialise the cell or [] with zeros to improve efficiency?
The situation I have is that I replace all the values above and below 'this variable'.
I need to save only 1 value each time and then reset all other variables(above and below it) to zero and repeat the loop.
If you initialize with zeros to improve efficiency, then you will need to use an index.
Your explanation of needing to replace values "above and below this variable" (what variable?) does not explain why using an index is not acceptable.
@walter: Please check the above question(updated).
please help-

请先登录,再进行评论。

Using the index "end+1" is often clearer coding. It is not clear why indexing should be avoided.
But anyhow, the following technically does not use any indexing:
results = [];
for K = 1 : 20
newvalue = rand(); %whatever the new value should be
results = [results; newvalue]; %add it to the end
end
This is not efficient (before R2011a anyhow.)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

提问:

2012-3-1

Community Treasure Hunt

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

Start Hunting!

Translated by