Finding differences between two vectors in an explicit way.

I am trying to compare two vectors, let's say
vec1 = [5 4 2 1 3 1 1 1 4];
vec2 = [5 4 2 5 3 2 1 2 2];
As you can see, they differ in indices 4, 6, 8, 9. I would like to determine how they differ, so that I get the following information:
The number 1 from vec1 is classified as a 5 in vec2 one time.
The number 1 from vec1 is classified as a 2 in vec2 two times.
The number 4 from vec1 is classified as a 2 in vec2 one time.
I would like to get it as a numerical result so that it is easier to work with, e.g.:
[1 5 1
1 2 2
4 2 1];
These vectors will be very long. Is there a fast way to go about this? Thank you all!

2 件のコメント

Ajay Kumar
Ajay Kumar 2019 年 10 月 27 日
Are there only single digit values in vec1 and vec2?
If not how are double and tripe digits are differed from vec1 and vec 2?
for example: how is 111 referred as? 555?
Jenna Wong
Jenna Wong 2019 年 10 月 27 日
Yes, only single digit values. In this case, where vec1 is a list of true class labels, and vec2 is a list of predicted class labels, I would expect 111 and 555 to be distinct separate classes, so they would just be the actual numbers:
vec1 = [5 4 2 1 3 1 1 1 4 111]

サインインしてコメントする。

 採用された回答

the cyclist
the cyclist 2019 年 10 月 27 日
編集済み: the cyclist 2019 年 10 月 27 日

0 投票

% Input vectors
vec1 = [5 4 2 1 3 1 1 1 4];
vec2 = [5 4 2 5 3 2 1 2 2];
% Identify the mismatched rows, and create a matrix of them
idx = vec1~=vec2;
mismatchedRows = [vec1(idx); vec2(idx)]';
% Find the *unique* mismatched rows, and the indices from the unique ones
% back to the original ones
[uniqueMismatchedRows,~,indexBacktoOriginal] = unique(mismatchedRows,'row','stable');
% Count the number of occurrences of each mismatch row
count = histcounts(indexBacktoOriginal,[unique(indexBacktoOriginal); Inf]);
% Put it all together for the output
output = [uniqueMismatchedRows count'];

1 件のコメント

Jenna Wong
Jenna Wong 2019 年 10 月 27 日
Thank you, that works perfectly! I need to learn how to use the function "unique" more often.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

製品

質問済み:

2019 年 10 月 27 日

コメント済み:

2019 年 10 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by