compare between element of matrix in different size

1 visualización (últimos 30 días)
Khairul Nur
Khairul Nur el 25 de Oct. de 2019
Respondida: Shubham Gupta el 25 de Oct. de 2019
hi, i need some help here. I want to compare element between two matrices new_mat[40x11] and kmatrix[1x4].
If the 11th columns of each row in new_mat == to any element in kmatrix, it will create an another matrix call cluster_k containing element column i until 10 from new_mat
for example new_mat
[11 12 13 14 15 16 17 18 19 13 1]
[11 12 13 14 15 16 17 18 19 14 2]
[11 12 13 14 15 16 17 18 19 15 3]
kmatrix [1 2 3 4]
so will have
cluster_1 [[11 12 13 14 15 16 17 18 19 13]
cluster_2 [11 12 13 14 15 16 17 18 19 14]
cluster_3[11 12 13 14 15 16 17 18 19 15 3]
Do appreciate some help.
Below is my current code:
new_mat = [C index]
i=1
kmatrix = [1:k]
for n = i:40
if (new_mat(i,11))== kmatrix[1:k] %stuck here
cluster_k = new_mat(i,10) % and here too
end
i=i+1
end

Respuestas (1)

Shubham Gupta
Shubham Gupta el 25 de Oct. de 2019
To check if there is any common element between 2 vectors, you may consider using 'intersect()'. So, your if condition becomes:
if isempty(intersect(new_mat(i,11)),kmatrix)
%% create cluster_k
end
Let me know if you have doubts !

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by