compare between element of matrix in different size

1 回表示 (過去 30 日間)
Khairul Nur
Khairul Nur 2019 年 10 月 25 日
回答済み: Shubham Gupta 2019 年 10 月 25 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by