Finding pair values in a matrix

조회 수: 4 (최근 30일)
Christina Petsa
Christina Petsa 2020년 5월 31일
편집: the cyclist 2020년 5월 31일
We had to calculate how many pairs we can find for the pythagorean triads for a,b,c when a^2+b^2=c^2. After we found those we had to create a matrix that included all possible pairs for a and b. For a=[1:100] and b=[1:200] I got 184 pairs. So now my matrix z is a 184x2 matrix, but there are definitely some pairs that are repeated. For example, when c=5 we get two possibilities x=3 and y=4, but also x=4 and y=3.
How can I make the code, so that if i have the rows (3,4) and (4,3), either one will be deleted and I can have a new matrix just for the possible pairs without caring which one is x or y?
I tried doing that with groupcounts(z) but that only works with vectors, not matrices. Any ideas?
  댓글 수: 2
the cyclist
the cyclist 2020년 5월 31일
편집: the cyclist 2020년 5월 31일
Can you say exactly what the input is, and exactly what you want the output to be?
Is it just, for example, that if you have a matrix
z = [1 2;
2 1;
3 4;
4 3;
5 6;
6 5];
that you want to return
z2 = [1 2;
3 4;
5 6];
?
It would be helpful if you gave a small, specific example that has enough generality to cover what you want to do.
You can upload data in a MAT file using, the paper-clip icon.
Christina Petsa
Christina Petsa 2020년 5월 31일
That is exactly what I meant. Sorry for not specifying enough!

댓글을 달려면 로그인하십시오.

답변 (2개)

Tommy
Tommy 2020년 5월 31일
How about this?
z = unique(sort(z,2),'rows');
  댓글 수: 3
Christina Petsa
Christina Petsa 2020년 5월 31일
Thank you!
Christina Petsa
Christina Petsa 2020년 5월 31일
It doesn't work the way we expected. If you don't mind I replied what the output is bellow, if you have any more ideas, I'd be thankful!

댓글을 달려면 로그인하십시오.


the cyclist
the cyclist 2020년 5월 31일
If my speculation in the comment above about what you want for z2 is correct, then
z2 = unique(sort(z,2),'row')
  댓글 수: 3
Christina Petsa
Christina Petsa 2020년 5월 31일
Hey, I just tried that but the result I get when I type that is two collumns of the same values.
I wanted what you explained above; if z=[3 4; 4 3; 5 2; 2 5] , i want to create a new one that is z2=[3 4; 5 2]
However with this I get a matrix that shows z2=[3 3; 4 4; 5 5;......] etc
Thanks in advance if you have any more ideas...
the cyclist
the cyclist 2020년 5월 31일
편집: the cyclist 2020년 5월 31일
Are you absolutely certain you typed the solution code exactly as we wrote it? Because it worked for me.
I'm guessing maybe you wrote
sort(z)
instead of
sort(z,2)
The correct syntax sorts along dimension 2.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by