Find nth value in a matrix and plot on graph
11 views (last 30 days)
Show older comments
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])
0 Comments
Accepted Answer
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)
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)
disp(all_vals)
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.
0 Comments
More Answers (1)
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
See Also
Categories
Find more on Graphics Performance 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!

