Can I use ismember to compare a vector with a matrix

15 views (last 30 days)
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)

Walter Roberson
Walter Roberson on 28 Aug 2020
any(array_sample(:) == matrix_sample,2)
  1 Comment
Gayan Lankeshwara
Gayan Lankeshwara on 28 Aug 2020
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.


Steven Lord
Steven Lord on 28 Aug 2020
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
Gayan Lankeshwara
Gayan Lankeshwara on 28 Aug 2020
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.
Steven Lord
Steven Lord on 28 Aug 2020
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.


Johan Löfberg
Johan Löfberg on 29 Aug 2020
You should post YALMIP specific question to YALMIP specific forums

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!