How can I use find to create multiple variables instead of one variable that is a long vector?

조회 수: 1 (최근 30일)
Hello, sorry if this is not the best way to do it but this is what comes to mind. I'm trying to identify a window of when a vector exceeds a value, and how for long. For instance, the last line creates a new vector of each cell in which y_lumbar_apa exceeds the M_L_Threshold (at the bottom). But what I really want is for it create a new variable for each period it passes the threshold (490-496 as one, 516 to 523 as another), because then I need to make sure that the new variable is longer (cell wise) than the min and less than the max. Again, sorry, I imagine I need a for loop in there, but for some reason I just can't wrap my head around any syntax that makes sense, so I would appreciate any help!
duration_min = 13;
duration_max = 192;
M_L_Lumbar_peak = max(y_lumbar_apa);
M_L_Threshold = M_L_Lumbar_peak * .2;
possible_APA_window = find(y_lumbar_apa > M_L_Threshold)
Example output:
possible_APA_window =
Columns 1 through 25
490 491 492 493 494 495 496 516 517 518 519 520 521 522 523 549 550 551 552 553 554 555 622 623 624
Columns 26 through 50
625 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
Columns 51 through 75
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
Columns 76 through 85
710 711 712 713 714 715 716 717 718 719

채택된 답변

ProblemSolver
ProblemSolver 2023년 6월 27일
Hello @GDT:
I wasn't sure if I understood correctly or not. But here is what I understood. Please find the code below:
duration_min = 13;
duration_max = 192;
M_L_Lumbar_peak = max(y_lumbar_apa);
Unrecognized function or variable 'y_lumbar_apa'.
M_L_Threshold = M_L_Lumbar_peak * 0.2;
possible_APA_window = find(y_lumbar_apa > M_L_Threshold);
windows = {}; % Cell array to store the identified windows
% Initialize the first window
current_window = possible_APA_window(1);
prev_index = possible_APA_window(1);
% Iterate through the indices
for i = 2:length(possible_APA_window)
if possible_APA_window(i) - prev_index == 1
% Continue adding to the current window
current_window = [current_window, possible_APA_window(i)];
else
% Start a new window
if length(current_window) >= duration_min && length(current_window) <= duration_max
windows{end+1} = current_window; % Store the previous window
end
current_window = possible_APA_window(i);
end
prev_index = possible_APA_window(i);
end
% Check the last window
if length(current_window) >= duration_min && length(current_window) <= duration_max
windows{end+1} = current_window; % Store the last window
end
% Display the identified windows
for i = 1:length(windows)
fprintf('Window %d: %d to %d\n', i, windows{i}(1), windows{i}(end));
end
The code is providing an error as I didn't access to the array y_lumbar_apa.
I hope this helps.

추가 답변 (1개)

DGM
DGM 2023년 6월 27일
편집: DGM 2023년 6월 27일
I'm not sure if this is what is being asked, but:
% some fake periodic data
t = linspace(0,4*pi,500);
y = abs(sin(t.^1.1));
% create mask based on some threshold
mk = y >= 0.8;
% find segment extents
dmk = diff([0 mk 0]);
starts = find(dmk==1);
ends = find(dmk==-1)-1;
% extract each segment into a cell array
nseg = numel(starts);
segments = cell(nseg,1);
for k = 1:nseg
segments{k} = y(starts(k):ends(k));
end
% for sake of illustration, plot the selected data segments
plot(t,y,':'); hold on % the original data
for k = 1:nseg
hp = plot(t(starts(k):ends(k)),y(starts(k):ends(k))); % each selected segment
hp.LineWidth = 2;
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by