Using logic to extract cells from another matrix
5 views (last 30 days)
Show older comments
I am trying to create a matrix by using logical operators. I have a time (frame number) column and two other variable columns (x,y)
I used the operators to determine a threshold and that returned the original matrix size.
After using the operators i have a new column matrix of 103 from my first variable.
The second variable didnt not have any numbers that fit out of the threshold so I got a column matrix of all zeros.
Next I found all the frames in which the x variable had a 1. So for time (Frame number) and my x variable I have a 103x1 column matrix.
My question is how can I use the frame number to create a column matrix with my corresponding y variable to get a 103x1 matrix so my matrix sizes match.
I am plotting the unedit x,y variable, then using the hold on command to plot all the values that deviate from the threshold limits over my exisitng plot.
Mx = median(COPD_data.AnteriorPosteriorSway_mm_) %median(Sortx)
My = median(COPD_data.MedioLateralSway_mm_) %median(Sorty)
Basex_plus = Mx+5 %mm
Basex_minus = Mx-5
Basey_plus = My+5 %mm
Basey_minus = My-5
Adjust_FN = COPD_data.FrameNumber(COPD_data.AnteriorPosteriorSway_mm_ >= Basex_minus &...
COPD_data.AnteriorPosteriorSway_mm_ >= Basex_plus)
Adjust_AP = COPD_data.AnteriorPosteriorSway_mm_(COPD_data.AnteriorPosteriorSway_mm_ >= Basex_minus &...
COPD_data.AnteriorPosteriorSway_mm_ >= Basex_plus)
Adjust_ML = COPD_data.MedioLateralSway_mm_(COPD_data.MedioLateralSway_mm_>= Basey_minus &...
COPD_data.MedioLateralSway_mm_>= Basey_plus)
plot (Adjust_AP,COPD_data.MedioLateralSway_mm_, 'r--')
Thank you in advance for the help!
2 Comments
Rik
on 24 Sep 2020
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.
Answers (1)
Raag
on 4 Jul 2025
Hi Thallon,
It is my understanding that you are trying to extract the corresponding y values for the frames where your x variable exceeds a threshold. You already have the frame numbers and x values filtered, and now you want the matching y values to complete the 103x1 matrix.
This can be achieved by using the same logical condition that is applied to the ‘x’ variable to index into the’ y’ variable. The following code can be used:
idx_x = COPD_data.AnteriorPosteriorSway_mm_ > Basex_plus | ...
COPD_data.AnteriorPosteriorSway_mm_ < Basex_minus;
Adjust_ML = COPD_data.MedioLateralSway_mm_(idx_x);
This ensures that ‘Adjust_ML’ will be the same size as your filtered ‘x’ values and frame numbers.
For more details, refer:
- Access data in MATLAB - https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-table.html
- How logical indexing works: https://www.mathworks.com/matlabcentral/answers/129-how-does-logical-indexing-work
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!