Removing values from two different variables

Hi,
I have attached two variables, AL.mat and QRS.mat. AL.mat is a 1x48 cell containing labels 0, 1 and 2. QRS.mat is also a 1x48 cell and it contains peak indices.
Basically, I want to remove all the 2's from the AL variable. Simultaneously, I want to remove the peak indices that correspond to these 2's in the QRS variable. How can I do that?

 Accepted Answer

% For each cell of AL, find the non-2's
keepIndices = cellfun(@(x)x~=2,AL,'UniformOutput',false);
% Keep the elements of AL that are non-2's
AL_no_2 = cellfun(@(x,y)x(y),AL, keepIndices,'UniformOutput',false);
% Keep the elements of QRS that are non-2's
QRS_no_2 = cellfun(@(x,y)x(y),QRS,keepIndices,'UniformOutput',false);

7 Comments

Thanks a lot. Works like a charm!
Hi again,
I have run into another problem. The thing is that by removing these 2's, I will have "gaps" in some of these QRS cells (AL cells as well but they are not that important here). What I mean by gaps is that the difference between some of the indices will be much larger than original because these 2's might be in the middle.
The QRS variable is used in the FeatureExtraction.m function that I have attached. This function calculates different features. They are all dependent on the difference between the indices in this new QRS variable. Is there any way, I can cope with this problem such that these gaps/jumps will not have an impact?
I really hope someone can help. Thanks!
I'm not really clear on the problem. Maybe you can clarify. Let's get specific.
I see, for example, that in the cell AL{3}, in the range AL{3}(111:135), there is a group of 2's surrounded by 0's. I assume the "gap" you are talking is caused by removing those 2's.
figure
subplot(1,2,1), bar(AL{3}(111:135))
title('AL\{3\}(111:135)');
subplot(1,2,2), bar(QRS{3}(111:135))
title('QRS\{3\}(111:135)');
Screen Shot 2020-02-11 at 11.40.40 AM.png
So, what do you want to happen instead of just removing all the 2's from AL, and the corresponding elements from QRS?
Uerm
Uerm on 11 Feb 2020
Edited: Uerm on 11 Feb 2020
Yes, exactly. The "gaps" occur as these 2's are sorrounded by 0's and 1's most of the time.
The thing is that after this removal, the difference between these indices in this new QRS (QRS_no_2) will now be larger since the 2's and their indices are removed.
The problem with this is that the FeatureExtraction.m function calculates features based on these differences. The removal of 2's will therefore have an influence on the features.
I do need to remove these 2, but is there some way I can compensate for these large gaps? Maybe shifting of the indices?
The problem with this is that I am training a model based on these features on one dataset and I will be testing this model on another dataset. However, the removal of 2's do not occur on the test set since the set does not have 2's at all.
I hope this makes sense.
I think only you can decide what "compensate for these large gaps" means.
To take a tiny example, suppose that one of the AL vectors was
AL{i} = [0 0 2 2 2 2 0 0];
and the corresponding QRS is
QRS{i} = [200 300 400 500 600 700 800 900];
The current algorithm would leave you with
AL_no_2{i} = [0 0 0 0];
and
QRS_no_2{i} = [200 300 800 900];
What would you want instead? What is the "correct" result? I'm not sure we can decide that for you.
I was thinking about shifting the samples. For instance, if we say that
QRS_no_2{i} = [50 100 240 390 500 1000 1100 1250]
we can see that there is a large gap between 500 and 1000. As you said this could be the area where the 2's are removed.
I was thinking about "shifting" the samples such that the sample 1000 will be something like the mean difference between 50 100 240 390 and 500 added to 500. If we say that the mean difference between these 5 points is 110, then instead of calling the next sample 1000, we will call it 610. The sample after this will be 710 (since 1100-1000 = 100) and so on.
Will this be possible to implement? It should be everywhere in all the relevant cells and no matter if the 2' were sorrounded by 0's or 1's.
Sure, it is possible to implement. But it won't be a few simple lines of code like your original question. And I don't think you can do the entire cell array at once. The steps will be something like
  • Define a for loop that processes each element of the cell array in turn
  • For each element of the cell array AL, find the indices of the 2's. (You could use the find command.)
  • Use those to determine the indices just before and after that stretch of 2's
  • Do the math on QRS that you describe above
In a manner of speaking, it is much more of an algorithm, than a couple slick MATLAB command.

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Asked:

on 10 Feb 2020

Commented:

on 13 Feb 2020

Community Treasure Hunt

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

Start Hunting!