How can I check if the columns of my matrix have value in common?
조회 수: 19 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Walter Roberson
2021년 9월 26일
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)
sum(M(:,6) == M(:,1:5), 2) == 5
댓글 수: 6
Walter Roberson
2021년 9월 26일
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
추가 답변 (2개)
yanqi liu
2021년 9월 26일
sir,may be use the follow code
clc
close all
clear all
% init matrix
M = randi([1 100], 10, 8)
% choose one column
use = 4;
% find value
Mu = M(:, use)
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
fprintf('\n');
yanqi liu
2021년 9월 27일
clc
close all
clear all
% init matrix
M = randi([1 100], 10, 8)
% choose one column
use = 4;
% find value
Mu = M(:, use)
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
fprintf('\n');
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!