saving values without index

8 次查看(过去 30 天)
zozo
zozo 2012-3-1
%***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 个)

Matt Tearle
Matt Tearle 2012-3-1
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 个评论
zozo
zozo 2012-3-1
@walter: Please check the above question(updated).

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2012-3-1
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.)

类别

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