How to filter the following vector from multiple vectors?

How to filter the following vector from multiple vectors?
I want to do the following for each matrix stored in M
suppose each vector is named as the following in Matrix M: [deltaX
deltaY
1]
I want to filter the vector that satisfy the following conditions:
if abs(deltaY) is less than threshold for example 32
then select that vectors/vector
after that if there are more than selected vectors, filter them as the following:
select the vector that has the min deltaX
*The out should be 23 separated vectors each corrosponding to its matrix

1 Comment

What does this mean: "suppose the each vector is named as the following in Matrix MM"?
Would it be impolite if I reply although I'm neither Matt J nor Star Strider?! Please avoid addressing specific persons in the forum except if you have really good reasons.

Sign in to comment.

 Accepted Answer

Jon
Jon on 19 May 2022
Edited: Jon on 19 May 2022
I think this does what you describe
% filter matrix mm
% parameters
threshold = 32
% extract the matrix out of the cell array MM
load MM.mat
mm = MM{1}
% find all of the columns where the value in the second row is over
% threshold
result1 = mm(:,abs(mm(2,:))>threshold)
% now pick the one with the smallest value of "delta x" (first row)
[~,idx] = min(result1(1,:));
result2 = result1(:,idx)
For the calculation of result2, which pick the one with the minimum value in the first row, I wasn't sure from your description if you wanted the minimum of the actual values, or the minimum absolute value. I picked the minimum, so it is one with a negative value. You can modify the code accordingly.

3 Comments

Oops, I read your description more carefully, and realized I selected columns which were greater than the threshold, you wanted ones that were less, so change the corresponding line to
result1 = mm(:,abs(mm(2,:))<=threshold)
Anyhow, it looks like my answer is very similar to @Jan's. By the time I posted my answer I guess he had already put his in. So I would go with @Jan's answer
@Jon, thank you so much.
Can you please help in iterating your code For all M matrix that I added to the question please.
% filter matrices in M.mat
% parameters
threshold = 32
% load the cell array of matrices to be filtered
load M.mat
% loop through matrices filtering each one
% accumulate results (selected column) in a matrix where each column is the
% selected column from the corresponding matrix in M
numFilter = numel(M); % number of matrices to be filtered
R = zeros(3,numFilter); % preallocate array to hold results
for k = 1:numFilter
% extract the kth matrix to be filtered
m = M{k};
% find all of the columns where the value in the second row is less than
% threshold
result1 = m(:,abs(m(2,:))<threshold);
% now pick the one with the smallest value of "delta x" (first row)
[~,idx] = min(result1(1,:));
% store the result
R(:,k) = result1(:,idx);
end

Sign in to comment.

More Answers (1)

Maybe:
V = MM(:, abs(MM(2, :)) < 32);
[~, index] = min(V(1, :));
W = V(:, index)

Asked:

M
M
on 19 May 2022

Edited:

Jon
on 19 May 2022

Community Treasure Hunt

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

Start Hunting!