how to stop overwriting results of a for loop in an array

조회 수: 5 (최근 30일)
John Campbell
John Campbell 2020년 9월 18일
댓글: John Campbell 2020년 9월 18일
Hello,
I know this is a common question, so sorry in advance, but the other responses have not been working with mine, probably because I haven't been Implementing them correctly, which is why I'm here. In the code below, i have a file that contains a variabe LAnkle, which is 205x4. I am only concerend with the first and fourth columns however. The goal is to find the trough points, where the previous and following values are greater, and put them in the array y. However, when I do this y only equates to one value, it seems to be overwritten everytime. When I use disp(x) in the script, it displays all values in the fourth column as well, not just the trough points. Any help is appreciated.
clear,clc
load LAnkle_tracking.mat
plot(LAnkle(:,1),LAnkle(:,4),'k.')
y=zeros(1,length(LAnkle));
for i=2:length(LAnkle)-1
x=LAnkle(i,4);
if LAnkle(i-1,4)>x<LAnkle(i+1,4)
y=(x);
end
end

답변 (1개)

KSSV
KSSV 2020년 9월 18일
clear,clc
load LAnkle_tracking.mat
plot(LAnkle(:,1),LAnkle(:,4),'k.')
y=zeros(1,length(LAnkle));
for i=2:length(LAnkle)-1
x=LAnkle(i,4);
if LAnkle(i-1,4)>x<LAnkle(i+1,4)
y(i)=(x);
end
end
  댓글 수: 1
John Campbell
John Campbell 2020년 9월 18일
This seems to make sense, but when I tried it it contained every value of LAnkle(:,4) except the first and last values, and seemed to ignore the if statement.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by