How can I check if the columns of my matrix have value in common?

1 view (last 30 days)
Agnese Chini
Agnese Chini on 26 Sep 2021
Answered: yanqi liu on 27 Sep 2021
Hi, I have a basic problem.
I have a matrix 7x21 double and I want to compare each column with the others. I want to understand if a value in the first column is repeated in the following 20 columns. Either a value from the sixth column in all five first or subsequent columns.

Accepted Answer

Walter Roberson
Walter Roberson on 26 Sep 2021
I am not certain what you are asking
Either a value from the sixth column in all five first or subsequent columns.
M = randi(2, 10, 8)
M = 10×8
2 1 1 2 1 2 2 1 1 2 1 1 1 1 2 1 1 2 1 2 1 2 1 1 1 2 1 2 1 1 2 1 1 1 1 2 2 2 2 1 2 2 2 1 2 1 1 1 2 2 2 2 2 2 1 1 2 2 2 2 2 2 2 1 2 2 2 1 2 1 1 1 2 2 2 2 1 1 2 2
sum(M(:,6) == M(:,1:5), 2) == 5
ans = 10×1 logical array
0 0 0 0 0 0 1 1 0 0
  6 Comments
Walter Roberson
Walter Roberson on 26 Sep 2021
When you ask "which" you might be asking what the location is, or you might be asking what the value is. My code returns 0 for locations that do not match and 1 for ones that match, so it answers the question about location.
any(matches, 2)
is 1 for rows that match.
to_compare(any(matches, 2))
will tell you the values but not the location

Sign in to comment.

More Answers (2)

yanqi liu
yanqi liu on 26 Sep 2021
sir,may be use the follow code
clc
close all
clear all
% init matrix
M = randi([1 100], 10, 8)
M = 10×8
7 36 81 22 55 59 50 4 74 14 62 38 7 3 78 43 7 38 43 90 12 11 7 53 38 10 58 46 90 4 37 3 33 29 93 20 98 8 30 36 59 26 84 36 100 6 56 96 13 56 68 25 51 39 42 69 38 19 89 23 1 96 33 49 24 100 84 39 41 50 39 64 55 86 48 44 9 12 48 72
% choose one column
use = 4;
% find value
Mu = M(:, use)
Mu = 10×1
22 38 90 46 20 36 25 23 39 44
M2 = M;
M2(:, use) = NaN;
% the different range
th = 0;
for i = 1 : length(Mu)
mi = Mu(i);
di = abs(M2 - mi);
[r, c] = find(di <= th);
fprintf('\n\nthe %d data of column %d is %d\n', i, use, mi);
if length(r) < 1
fprintf('\n can not find other column has this value!');
end
for j = 1 : length(r)
fprintf('\nM(%d,%d)=%d', r(j), c(j), M(r(j), c(j)));
end
end
the 1 data of column 4 is 22
can not find other column has this value!
the 2 data of column 4 is 38
M(4,1)=38 M(8,1)=38 M(3,2)=38
the 3 data of column 4 is 90
M(4,5)=90
the 4 data of column 4 is 46
can not find other column has this value!
the 5 data of column 4 is 20
can not find other column has this value!
the 6 data of column 4 is 36
M(1,2)=36 M(5,8)=36
the 7 data of column 4 is 25
can not find other column has this value!
the 8 data of column 4 is 23
can not find other column has this value!
the 9 data of column 4 is 39
M(7,6)=39 M(9,7)=39
the 10 data of column 4 is 44
can not find other column has this value!
fprintf('\n');
  2 Comments
Agnese Chini
Agnese Chini on 26 Sep 2021
This are the question about your code:
for i = 1 : length(Mu)
mi = Mu(i);
di = abs(M2 - mi);
[r, c] = find(di <= th);
fprintf('\n\nthe %d data of column %d is %d\n', i, use, mi);
if length(r) < 1
fprintf('\n can not find other column has this value!');
else
fprintf('\n I find the same value in here!'); %I want to know where is it the same value and wich is the value
end
for j = 1 : length(r)
fprintf('\nM(%d,%d)=%d', r(j), c(j), M(r(j), c(j))); %What did you print here?
end
end

Sign in to comment.


yanqi liu
yanqi liu on 27 Sep 2021
clc
close all
clear all
% init matrix
M = randi([1 100], 10, 8)
M = 10×8
85 51 44 55 49 2 1 73 5 99 30 99 77 99 6 50 6 92 97 49 35 57 24 44 27 96 96 45 69 8 89 94 64 29 82 78 13 89 77 9 1 41 53 91 33 87 12 30 52 64 85 17 54 62 63 58 74 64 21 94 28 87 80 16 56 58 77 16 59 28 94 91 96 83 2 84 54 64 39 1
% choose one column
use = 4;
% find value
Mu = M(:, use)
Mu = 10×1
55 99 49 45 78 91 17 94 16 84
M2 = M;
M2(:, use) = NaN;
% the different range
th = 0;
for i = 1 : length(Mu)
mi = Mu(i);
di = abs(M2 - mi);
[r, c] = find(di <= th);
fprintf('\n\n the %d data of column %d is %d\n', i, use, mi);
if length(r) < 1
fprintf('\n can not find other column has this value!');
continue;
else
fprintf('\n I find the same value in here! Please view the follow %d data for detail!', length(r));
end
for j = 1 : length(r)
fprintf('\n M(%d,%d)=%d', r(j), c(j), M(r(j), c(j)));
end
end
the 1 data of column 4 is 55
can not find other column has this value!
the 2 data of column 4 is 99
I find the same value in here! Please view the follow 2 data for detail!
M(2,2)=99 M(2,6)=99
the 3 data of column 4 is 49
I find the same value in here! Please view the follow 1 data for detail!
M(1,5)=49
the 4 data of column 4 is 45
can not find other column has this value!
the 5 data of column 4 is 78
can not find other column has this value!
the 6 data of column 4 is 91
I find the same value in here! Please view the follow 1 data for detail!
M(9,8)=91
the 7 data of column 4 is 17
can not find other column has this value!
the 8 data of column 4 is 94
I find the same value in here! Please view the follow 2 data for detail!
M(9,7)=94 M(4,8)=94
the 9 data of column 4 is 16
I find the same value in here! Please view the follow 1 data for detail!
M(8,8)=16
the 10 data of column 4 is 84
can not find other column has this value!
fprintf('\n');

Community Treasure Hunt

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

Start Hunting!