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!