Can I use ismember to compare a vector with a matrix
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (3)
Steven Lord
el 28 de Ag. de 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 comentarios
Steven Lord
el 28 de Ag. de 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.
Johan Löfberg
el 29 de Ag. de 2020
You should post YALMIP specific question to YALMIP specific forums
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!