Can I use ismember to compare a vector with a matrix

Hi there,
Imagine I have an array and a matrix as below.
array_sample = [10;20;30]
matrix_sample = [15,35,10;22,33,44;55,25,30]
both array_sample and array_sample have the same number of rows.
Can anyone please tell me how to use ismember to compare each row in array_sample with each row in matrix_sample and output 1 if the array_sample element is present in the corresponding row of the matrix_sample ?
For the example given above the output should be
%% if I try something like this,
sample_out = ismemeber(array_sample, matrix_sample)
%% the output should be like this
sample_out = [1,0,1]' ; %% Because 10 and 30 are found respectively in 1st and 3rd row of matrix_sample.
Thank you.

Answers (3)

any(array_sample(:) == matrix_sample,2)

1 Comment

Thanks a lot for your response.
This actually works perfect.
But my overall intention is something different.
Actually I am formulating constraints for an optimisation problem (using YALMIP)
So, array_sample is my decision variable (which I want to optimise). So I do not know the values of it.
%% assume I have loaded YALMIP to matlab path
nu = 3; %% matched with the previous MWE
N = 10 ; %% arbitrary value
%% matrix_sample
matrix_sample = [15,35,10;22,33,44;55,25,30];
%% decision variable
array_sample = sdpvar(repmat(nu,1,N), repmat(1,1,N)); %% spdvar is a YALMIP function.
constraints = [];
for k = 1 : N
%% for each iteration of k, I need array_sample to take any value from matrix_sample
constraints = [constraints, array_sample{k}, matrix_sample]; %% this line is wrong, only tells what I want
end
Please mind that the above code is not a MWE. (since it includes YALMIP toolbox plugin)
But would really appreciate if you could at least give me hint on how the assignment of array_sample to take values from matrix_sample can be achieved ?
Thank you.

Sign in to comment.

Since you're using release R2019a (which supports implicit expansion) and your data is compatibly sized this is a job for the == operator and the any function.
array_sample = [10;20;30];
matrix_sample = [15,35,10;22,33,44;55,25,30];
sample_out = any(array_sample == matrix_sample, 2)

2 Comments

Thanks a lot for your response.
This actually works perfect.
But my overall intention is something different.
Actually I am formulating constraints for an optimisation problem (using YALMIP)
So, array_sample is my decision variable (which I want to optimise). So I do not know the values of it.
%% assume I have loaded YALMIP to matlab path
nu = 3; %% matched with the previous MWE
N = 10 ; %% arbitrary value
%% matrix_sample
matrix_sample = [15,35,10;22,33,44;55,25,30];
%% decision variable
array_sample = sdpvar(repmat(nu,1,N), repmat(1,1,N)); %% spdvar is a YALMIP function.
constraints = [];
for k = 1 : N
%% for each iteration of k, I need array_sample to take any value from matrix_sample
constraints = [constraints, array_sample{k}, matrix_sample]; %% this line is wrong, only tells what I want
end
Please mind that the above code is not a MWE. (since it includes YALMIP toolbox plugin)
But would really appreciate if you could at least give me hint on how the assignment of array_sample to take values from matrix_sample can be achieved ?
Thank you.
So you want to evaluate your objective function on a column vector where each element must come from the corresponding row of the matrix? So for your example matrix_sample your optimization routine would be allowed to evaluate the objective function with input [15; 33; 55] but not [15; 33; 54] (since 54 is not in the third row of matrix_sample)?
That's a very different question than the one you asked.

Sign in to comment.

Products

Release

R2019a

Asked:

on 28 Aug 2020

Commented:

on 29 Aug 2020

Community Treasure Hunt

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

Start Hunting!