compare between element of matrix in different size

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

回答(1 个)

Shubham Gupta
Shubham Gupta 2019-10-25
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 !

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by