I have this matrix:
2 2 82 10 16 15 66 76 71 83 44 49
4 2 91 28 98 43 4 75 4 70 39 45
4 1 13 55 96 92 85 40 28 32 77 65
1 2 92 96 49 80 94 66 5 96 80 71
1 2 64 97 81 96 68 18 10 4 19 76
the code should look to first column,if its not repeated,gives the output:
8 1 6 7 5 10 9 3 4 2 which is the indices after sorting in descending way.
if its repeated,look to the second column if 1 ,the code should sort the elements of second line and distribute them as 1:7 to original and the rest to the repetition
if the of second column is 2 ,the code should divide 1:5 to first and 5:10 to the second.
the result should look like:
8 1 6 7 5 10 9 3 4 2
3 1 6 8 10 0 0 0 0 0
4 9 2 5 7 0 0 0 0 0
2 8 5 1 4 0 0 0 0 0

 채택된 답변

Geoff Hayes
Geoff Hayes 2014년 5월 4일

1 개 추천

yousef - the easiest part of the above problem is to get a matrix of sorted indices that correspond to the data in each row sorted in descending order. Consider using the sort command (type help sort in the command window for details). With it, you can sort as follows:
Z = [2 2 82 10]; % your 5x12 input matrix
% ignore (~) sorted data and consider the indices (Idcs) only, sorting on all rows of Z but only
% on columns 3 through the end; we sort on the second dimension (2) which is the columns in
% descending order
>>[~,Idcs] = sort(Z(:,3:end),2,'descend');
>> Idcs
Idcs =
8 1 6 7 5 10 9 3 4 2
3 1 6 8 10 4 9 2 5 7
3 4 5 9 10 2 6 8 7 1
2 8 5 1 4 9 10 6 3 7
2 4 3 10 5 1 9 6 7 8
Now you can iterate over each element in the first row of Z, checking to see how many times that element is repeated in that first column (I think that is your requirement). You can cound the number of times an element is repeated by using a combination of the length and find commands (left to you). If not repeated, then do nothing since the row of Idcs is already sorted in descending order by indices. If it is repeated, then consider the second column and check for its repetition using length and find. If not repeated, then apply your first rule (which is a little fuzzy - why consider 1:7 and what is repetition? If it is repeated, then apply your second rule which is a little fuzzy too - are you leaving the first five indices in the current row, and moving the remaining five indices to the subsequent row? (Or that row which has the repeated second column element? Is that why your final answer has only four rows when the input has five?

댓글 수: 5

yousef Yousef
yousef Yousef 2014년 5월 4일
Thanks a lot.First I have to say that its unbelievable how you understand my question.I thought no body will understand it because I didn't explain it well.Also its complicated. second,yes the solution starts from dividing the matrix how you did.then start looking for the first 2 columns. I just have one more question,should I use while loop instead of for loop because in one step I want to jump 2 rows? Thanks
Geoff Hayes
Geoff Hayes 2014년 5월 4일
In an interesting problem, so kind of fun! Try using a while loop. That will allow you to control over which rows in the first column you are iterating over and give you the chance to skip when necessary.
Image Analyst
Image Analyst 2014년 5월 5일
What's the use case for this? Is there a real world use for this kind of complicated algorithm?
I got to admit I didn't understand, and still don't. You said look to the first column, [2;4;4;1;1], and see if it's repeated. No, it's not - that column does not appear anywhere else. So now the output should be a 1 by 10 row vector [8 1 6 7 5 10 9 3 4 2], which you say is the sorting indexes of the column sorted in a descending way. Well there are only 5 numbers in that column so how you get 10 numbers out, I have no idea. Well whatever... at least Geoff understood it and was able to help you. I voted for his miraculous achievement.
yousef Yousef
yousef Yousef 2014년 5월 5일
Hi,Thanks Geof.I called the result Geof instead of Idcs. I did it with while lop and it works fine.
Thanks a lot
Geoff Hayes
Geoff Hayes 2014년 5월 5일
Anytime!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

태그

질문:

2014년 5월 4일

댓글:

2014년 5월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by