Hello. I have a question. If you can help me, i am pleasure.
Question: Have any matrix's column same number?
In my idea, my code just find number of diagonal.
clc;
clear;
tic
sum=0;
A=[1 2 3;4 5 3;7 8 9];
B=size(A);
m=B(1,1);
n=B(1,2);
for i=1:m
for j=1:n
if((A(i,j) == A(j,j) || A(j,i) == A(i,i)) && i ~= j )
sum=sum+1;
end
end
end
if sum==0
disp('different')
else
disp('same')
end
A
toc

7 件のコメント

James Tursa
James Tursa 2015 年 5 月 19 日
編集済み: James Tursa 2015 年 5 月 19 日
I don't understand the question completely. Is your current code not producing the result you want? Are you trying to find out if any diagonal elements are duplicated in the same column? Are you trying to generalize your current code? Or what? Can you give a numeric example?
Mehmet Gurturk
Mehmet Gurturk 2015 年 5 月 19 日
編集済み: Mehmet Gurturk 2015 年 5 月 19 日
For example, you enter [3x3] matrix. My code find of same numbers to a column. If find same numbers, say 'same' but don't find, say 'different'.
Stephen23
Stephen23 2015 年 5 月 19 日
@Mehmet Gurturk: the explanation is not completely clear. Can you please give us an exact example, with numbers, like this:
A = [1,2,3;4,5,6;1,8,9]
what should happen? What output do you want?
Mehmet Gurturk
Mehmet Gurturk 2015 年 5 月 19 日
編集済み: Image Analyst 2015 年 5 月 19 日
For example,
A = [ 1 2 3;
4 5 6;
7 8 9] output = Different
A = [ 1 2 3;
4 5 6;
1 8 9] output = Same
But
A = [ 1 2 *3* ;
4 5 *3* ;
7 8 9] output = *Different*
I want to do
A = [ 1 2 *3* ;
4 5 *3* ;
7 8 9] output = *Same*
Mehmet Gurturk
Mehmet Gurturk 2015 年 5 月 19 日
My code just find to numbers of diagonal matrix
Joseph Cheng
Joseph Cheng 2015 年 5 月 19 日
Stephen he is trying to say that in the code he has presented it marks the second example as "different" due to the fact that his code only checks the diagonals.
Stephen23
Stephen23 2015 年 5 月 19 日
編集済み: Stephen23 2015 年 5 月 19 日
@Joseph Cheng: thank you for the clarification, I was a little bit confused about the meaning.

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

回答 (3 件)

Joseph Cheng
Joseph Cheng 2015 年 5 月 19 日
編集済み: Joseph Cheng 2015 年 5 月 19 日

1 投票

you can use the function unique() to determine if there are repeated values within the matrix
x = randi(10,4,4);
disp(x);
for ind = 1:size(x,2)
if numel(unique(x(:,ind)))<numel(x(:,ind))
disp('same')
else
disp('different')
end
end

1 件のコメント

Joseph Cheng
Joseph Cheng 2015 年 5 月 19 日
if you only want 1 answer then
x = randi(10,4,4);
disp(x);
same = 0;
for ind = 1:size(x,2)
if numel(unique(x(:,ind)))<numel(x(:,ind))
same = same+1;
end
end
if same>0
disp('same');
else
disp('different');
end

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

Stephen23
Stephen23 2015 年 5 月 19 日
編集済み: Stephen23 2015 年 5 月 19 日

0 投票

Here is a very fast and simple solution that does not require any loops (i.e. fully vectorized code). An output of 0 indicates different, and output of 1 indicates same.
>> A = [1,2,3; 4,5,6; 7,8,9]
A =
1 2 3
4 5 6
7 8 9
>> any(any(0==diff(sort(A))))
ans =
0
>> A = [1,2,3; 4,5,6; 1,8,9]
A =
1 2 3
4 5 6
1 8 9
>> any(any(0==diff(sort(A))))
ans =
1
Here it is on your second example matrix:
>> A = [1,2,3; 4,5,3; 7,8,9]
A =
1 2 3
4 5 3
7 8 9
>> any(any(0==diff(sort(A))))
ans =
1
And if you want to know which column contains repeated values then simply remove one of the any commands:
>> any(0==diff(sort(A)))
ans =
0 0 1
This code works very simply: it sorts the columns, then uses diff to find if any adjacent values are the same. You can use the all(..) to control the if statement, like this:
if all(...)
disp('same')
else
disp('different')
end

1 件のコメント

Joseph Cheng
Joseph Cheng 2015 年 5 月 19 日
very nice.. I like the use of sort.

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

Mehmet Gurturk
Mehmet Gurturk 2015 年 5 月 20 日

0 投票

Thank you so much :)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

質問済み:

2015 年 5 月 19 日

回答済み:

2015 年 5 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by