Can I use ismember to compare a vector with a matrix

14 次查看(过去 30 天)
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.

回答(3 个)

Walter Roberson
Walter Roberson 2020-8-28
any(array_sample(:) == matrix_sample,2)
  1 个评论
Gayan Lankeshwara
Gayan Lankeshwara 2020-8-28
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 2020-8-28
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 个评论
Gayan Lankeshwara
Gayan Lankeshwara 2020-8-28
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 2020-8-28
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.

请先登录,再进行评论。


Johan Löfberg
Johan Löfberg 2020-8-29
You should post YALMIP specific question to YALMIP specific forums

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by