Find nth value in a matrix and plot on graph

11 views (last 30 days)
I have a program that plots a graph and marks 4 dots on each line represening +/- 2mph. I have it figured it out how to place the dots for the beginning and the end but not values 2 through n. I am using the find command but I am having a hard time getting it to find values 2 throught n. Here is the code I am using (will need VEX file that I have attached to run):
Run=6;
i72=zeros([max(Run) 1]); i68=i72; i62=i72; i58=i72; i22=i72; i18=i72; i12=i72; i08=i72;
i=1;
while i<=max(Run)
if i==1
i72(i)=find(VEX>=71 & VEX<=72,1,'first');
i68(i)=find(VEX>67 & VEX<68,1,'first');
i62(i)=find(VEX>61 & VEX<=62,1,'first');
i58(i)=find(VEX>57 & VEX<58,1,'first');
i22(i)=find(VEX<=22,1,'first');
i18(i)=find(VEX<18,1,'first');
i12(i)=find(VEX<=12,1,'first');
i08(i)=find(VEX<8,1,'first');
i=i+1;
elseif i>1 && i~=max(Run)
% Need to Fix
i72(i)=find(VEX<=72,1);
i68(i)=find(VEX<68,1);
i62(i)=find(VEX<=62,1);
i58(i)=find(VEX<58,1);
i22(i)=find(VEX<=22,1);
i18(i)=find(VEX<18,1);
i12(i)=find(VEX<=12,1);
i08(i)=find(VEX<8,1);
i=i+1;
else
i72(i)=find(VEX>=72,1,'last');
i68(i)=find(VEX>68,1,'last');
i62(i)=find(VEX>=62,1,'last');
i58(i)=find(VEX>58,1,'last');
i22(i)=find(VEX>=22,1,'last');
i18(i)=find(VEX>18,1,'last');
i12(i)=find(VEX>=12,1,'last');
i08(i)=find(VEX>8,1,'last');
i=i+1;
end
end
i70=round((i72+i68)/2); i60=round((i62+i58)/2); i20=round((i22+i18)/2); i10=round((i12+i08)/2); % calculate Segment end points: 70, 60, 20, 10 mph
plot(VEX,'b.'); hold on; grid on
plot(i08,VEX(i08),'r*',i12,VEX(i12),'r*',i18,VEX(i18),'r*',i22,VEX(i22),'r*',i58,VEX(i58),'r*',i62,VEX(i62),'r*',i68,VEX(i68),'r*',i72,VEX(i72),'r*')
xlim([0 70000])

Accepted Answer

Voss
Voss on 29 Aug 2023
Edited: Voss on 29 Aug 2023
Is something like this what you're trying to do?
load VEX.mat
% plot VEX
figure
plot(VEX,'b.')
First, identity where VEX has large jumps up or down, from one segment to the next:
jump_threshold = 10;
idx = [true; abs(diff(VEX))>jump_threshold; true];
idx = find(idx)
idx = 10×1
1 1375 1837 3205 4579 5041 6409 7783 8245 9613
and plot (for reference) the starts (in green) and ends (in red) of the separate segments:
figure
plot(VEX,'b.')
hold on
plot(idx(1:end-1),VEX(idx(1:end-1)),'g.')
plot(idx(2:end)-1,VEX(idx(2:end)-1),'r.')
Now identify the value (and corresponding index of VEX) in each segment nearest to each of 8, 12, 18, 22, 58, 62, 68, and 72:
vals = [8 12 18 22 58 62 68 72];
NV = numel(vals);
NI = numel(idx)-1;
all_idx = NaN(NI,NV);
all_vals = NaN(NI,NV);
for ii = 1:NI
% subVEX is the data for the ii-th segment:
subVEX = VEX(idx(ii):idx(ii+1)-1);
for jj = 1:NV
% find the element nearest vals(jj) in segment ii
[d,min_idx] = min(abs(subVEX-vals(jj)));
% only store it if it is closer than jump_threshold away from vals(jj)
% (e.g., don't want to match the bottom point of a segment that is
% entirely above 50 to the value 22):
if d <= jump_threshold
all_idx(ii,jj) = min_idx+idx(ii)-1;
all_vals(ii,jj) = subVEX(min_idx);
end
end
end
This is what the indices and VEX values at those points are. (NaNs occur where the nearest point in a segment was further than jump_threshold away from vals(jj)):
disp(all_idx)
1274 922 466 161 NaN NaN NaN NaN NaN NaN NaN NaN 1756 1648 1502 1412 3093 2743 2223 1922 NaN NaN NaN NaN 4478 4126 3670 3365 NaN NaN NaN NaN NaN NaN NaN NaN 4960 4852 4706 4616 6297 5947 5427 5126 NaN NaN NaN NaN 7682 7330 6874 6569 NaN NaN NaN NaN NaN NaN NaN NaN 8164 8056 7910 7820 9501 9151 8631 8330 NaN NaN NaN NaN
disp(all_vals)
7.9984 12.0024 17.9914 21.9875 NaN NaN NaN NaN NaN NaN NaN NaN 58.0005 62.0110 68.0224 71.9748 8.0040 12.0086 17.9910 22.0014 NaN NaN NaN NaN 7.9984 12.0024 17.9914 21.9875 NaN NaN NaN NaN NaN NaN NaN NaN 58.0005 62.0110 68.0224 71.9748 8.0040 12.0086 17.9910 22.0014 NaN NaN NaN NaN 7.9984 12.0024 17.9914 21.9875 NaN NaN NaN NaN NaN NaN NaN NaN 58.0005 62.0110 68.0224 71.9748 8.0040 12.0086 17.9910 22.0014 NaN NaN NaN NaN
Plot the non-NaN elements of those:
figure
plot(VEX,'b.')
hold on
to_plot = ~isnan(all_idx);
plot(all_idx(to_plot),all_vals(to_plot),'r*')
Note that your approach of using find(__,1,'first') and find(__,1,'last') would never work on the interior segments since you are searching in the entire VEX vector. That's why I break VEX into segments first and search each segment individually.

More Answers (1)

Torsten
Torsten on 29 Aug 2023
Run = 6;
i72=zeros([max(Run) 1]); i68=i72; i62=i72; i58=i72; i22=i72; i18=i72; i12=i72; i08=i72;
for i = 1:Run
if i==1
i72(i)=find(VEX>=71 & VEX<=72,1,'first');
i68(i)=find(VEX>67 & VEX<68,1,'first');
i62(i)=find(VEX>61 & VEX<=62,1,'first');
i58(i)=find(VEX>57 & VEX<58,1,'first');
i22(i)=find(VEX<=22,1,'first');
i18(i)=find(VEX<18,1,'first');
i12(i)=find(VEX<=12,1,'first');
i08(i)=find(VEX<8,1,'first');
elseif i>1 && i<Run
% Need to Fix
i72(i)=find(VEX<=72,1);
i68(i)=find(VEX<68,1);
i62(i)=find(VEX<=62,1);
i58(i)=find(VEX<58,1);
i22(i)=find(VEX<=22,1);
i18(i)=find(VEX<18,1);
i12(i)=find(VEX<=12,1);
i08(i)=find(VEX<8,1);
else
i72(i)=find(VEX>=72,1,'last');
i68(i)=find(VEX>68,1,'last');
i62(i)=find(VEX>=62,1,'last');
i58(i)=find(VEX>58,1,'last');
i22(i)=find(VEX>=22,1,'last');
i18(i)=find(VEX>18,1,'last');
i12(i)=find(VEX>=12,1,'last');
i08(i)=find(VEX>8,1,'last');
end
end
  1 Comment
Ryan McCormack
Ryan McCormack on 29 Aug 2023
Edited: Ryan McCormack on 29 Aug 2023
This did not fix the issue. The graphs inbetween do not have the red dots on them and the first line has 5 dots on it (current graph). The second graph is what is suppose to look like (graph.jpg).

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!