フィルターのクリア

looping through symmetric matrices

1 回表示 (過去 30 日間)
MiauMiau
MiauMiau 2014 年 6 月 18 日
編集済み: Andrei Bobrov 2014 年 6 月 19 日
Hi
I have two matrices T and Q, and I want to plot the corresponding elements of them such as in:
T = randi(10,10)
Q = randi(10,10)
hold on
for i = 1:10
for j = 1:10
plot(T(i,j),Q(i,j))
end
end
But the thing is that the matrices are symmetric, and for reasons of complexity I do not want to go through redundant elements twice..what can I do? Thanks

回答 (3 件)

J. van Delft
J. van Delft 2014 年 6 月 18 日
Hi,
First of all the matrices are not symmetric. If they would be multiplying T with its transpose T' would give an identity matrix, which is not the case.
If I understand your question correctly you do not want to plot elements in T and Q that are equal? What you could do then is add a comparison in the inner for-loop. If the elements are equal, break the loop:
T = randi(10,10)
Q = randi(10,10)
hold on
for i = 1:10
for j = 1:10
if T(i,j) == Q(i,j)
break
end
plot(T(i,j),Q(i,j))
end
end
Let me know if this answers your question.
J. van Delft
  2 件のコメント
MiauMiau
MiauMiau 2014 年 6 月 19 日
well this was example code. the matrices in my code are symmetric.
MiauMiau
MiauMiau 2014 年 6 月 19 日
No, so I want to plot all the Elements of T and Q which have the same index but if the pairs happen to have values as already encountered I do not want them to be considered.. so for [1 4; 4 5] and [2 3; 3 6] the pairs to be plottet would be (1/2), (4/3),(4/3),(5/6) but the repeated (4/3) should not be considered...

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


Andrei Bobrov
Andrei Bobrov 2014 年 6 月 19 日
編集済み: Andrei Bobrov 2014 年 6 月 19 日
T = randi(20,10); %
Q = randi(20,10); %
T = tril(T) + tril(T,-1)'; %
Q = triu(Q) + triu(Q,1)'; % here we creating symmetric matrices
tt = tril(ones(size(T))) > 0;
plot(T(tt),Q(tt));

Joseph Cheng
Joseph Cheng 2014 年 6 月 18 日
編集済み: Joseph Cheng 2014 年 6 月 18 日
What you can do is use the unique() function
example:
T=randi(10,10);
Q=randi(10,10);
temp = [T(:) Q(:)];
utemp = unique(temp,'rows','legacy');
figure,plot(utemp(:,1),utemp(:,2),'.');
the unique will get rid of any repeating pairs leaving only unique elements.
  1 件のコメント
MiauMiau
MiauMiau 2014 年 6 月 19 日
thats not doing what i want at all..I always want to plot the elements with the same index, and if these pairs are repeated they should not be plotted..

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

カテゴリ

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