How to create a loop function within the same array?
1 回表示 (過去 30 日間)
古いコメントを表示
Hello, I'm trying to operate with an array of over 100000 data points (i.e. "k"). I have one array with the ID (i.e."ID") of the point and a second array with the data (i.e. "E")corresponding to each specific ID. The idea is that once an ID is equal to the previous value then it would to an operation that subtracts values from the array "E" creating the array "DE"; else, if the ID is different from previous value then "DE" is Zero.
DE = zeros(k,1);
for i=2:k
if ID(i,1)==ID(i-1,1)
DE(i,1)=(E(i,1)-E(i-1,1))*1000;
else
DE(i,1)=0;
end
end
The problem that I have is that the array "DE" becomes a column of zeros of size k.
0 件のコメント
回答 (1 件)
James Tursa
2017 年 6 月 27 日
編集済み: James Tursa
2017 年 6 月 27 日
A vectorized version (assuming I understand your dimensions correctly):
diffI = [1;diff(ID(:,1))];
diffE = [0;diff(E(:,1))]*1000;
DE = zeros(k,1);
x = diffI==0;
DE(x) = diffE(x);
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!