Effacer les filtres
Effacer les filtres

Can I use ismember to compare a vector with a matrix

14 vues (au cours des 30 derniers jours)
Gayan Lankeshwara
Gayan Lankeshwara le 28 Août 2020
Commenté : Gayan Lankeshwara le 29 Août 2020
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.

Réponses (3)

Walter Roberson
Walter Roberson le 28 Août 2020
any(array_sample(:) == matrix_sample,2)
  1 commentaire
Gayan Lankeshwara
Gayan Lankeshwara le 28 Août 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.

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 28 Août 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 commentaires
Gayan Lankeshwara
Gayan Lankeshwara le 28 Août 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 le 28 Août 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.

Connectez-vous pour commenter.


Johan Löfberg
Johan Löfberg le 29 Août 2020
You should post YALMIP specific question to YALMIP specific forums
  1 commentaire
Gayan Lankeshwara
Gayan Lankeshwara le 29 Août 2020
Thank you for the information. I will do.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by