How to create a loop function within the same array?
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
James Tursa
on 27 Jun 2017
Edited: James Tursa
on 27 Jun 2017
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);
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!