How do I extract from array with values I got from that same array?

3 views (last 30 days)
I have ICA.txt file that has all the data (2 x 205) I need for this problem. I made a function that takes that data and uses the second column to find the peak.
function peak_indicies = peaks(height)
peak_indicies =[];
for i = 2:length(height)-1
if (height(i) > height(i-1))&&(height(i) > height(i+1))
peak_indicies(end+1) = i;
else
continue;
end
end
It works and I have 3 peaks, but I am stuck trying to extract both the second column and the first column so that I can plot those three points. I tried in script
peaktime = [];
while i = 1:205
if peak_indicies == ICA(:,2)
peaktimes = i
else
continue;
end
end
But this didn't work and I am not sure how else to do it. I tried
peaktimes = extract(ICA(ICA(:,2)==peak_indicies))
%Error using extract
%Not enough input arguments.
But also I am unsure how I would use that in this situation.

Answers (1)

Voss
Voss on 9 Mar 2023
% call peaks() to get the indices of the peak(s), based on the 2nd column of ICA:
peak_indices = peaks(ICA(:,2));
% get the 1st and 2nd column values (times and heights I guess) at those row indices:
peak_vals = ICA(peak_indices,:);
% or if you want them in two separate variables
peaktimes = ICA(peak_indices,1);
peakheights = ICA(peak_indices,2);

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!