How to use logical indexing to return elements of a signal?
Ältere Kommentare anzeigen
Hi all,
I have a signal like this (attached). My ultimate aim is to return elements of the signal between parts of the signal that are empty and put them in seperate cells as depicted on the image on the right. My approach and a problem is below. Would you mind helping?

load 'signal'
% Replace nan values with 0
force_y_r(isnan(force_y_r))=0;
% find peaks of the both sides of the flat section of the signal
idx_first = islocalmin(force_y_r, 'FlatSelection','first', 'MinSeparation',500, 'MinProminence',750);
idx_last = islocalmin(force_y_r, 'FlatSelection','last', 'MinSeparation',500, 'MinProminence',750);

% THE PROBLEM: find column numerical values in force_y_r that corresponds to idx_first (green) & % idx_last (red)
[row_idx_first,col_idx_first] = find(size(force_y_r),idx_first);
[row_idx_last,col_idx_last] = find(size(force_y_r),idx_last);
this gives me:
Error using find
Second argument must be a positive scalar integer.
% extract parts of the signal between col_idx_first(1) and col_idx_last(1)% col_idx_first(2) and col_idx_last(2)
% and so on
%if col_idx_last occured before col_idx_first
col_idx_last(1) = []
n_start = numel(col_idx_first)-1;
force_y_r_cycle = cell(n_start,1) ;
for i = 1:n_start
force_y_r_cycle{i} = force_y_l(col_idx_first(i):col_idx_last(i+1)) ;
end
Akzeptierte Antwort
Weitere Antworten (1)
dpb
am 14 Jul. 2022
>> whos -file signal
Name Size Bytes Class Attributes
force_y_r 9607x1 76856 double
>>
Your signal is just a vector, not a 2D array so all you need is one-element addressing -- the column is always 1. And, since islocalmin returns a logical array, everything is zero except the true locations and so
idx1=find(idx_first);
idx2=find(idx_last);
is the set of indices you're looking for. I'd not bother with the second variable here, probably, but just overwrite the two you already have. Or, you could write
idx_first=find(islocalmin(force_y_r, 'FlatSelection','first', 'MinSeparation',500, 'MinProminence',750));
from the start.
Kategorien
Mehr zu Descriptive Statistics finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!