Multidimensional Array deleting entire max record keeping information across in order
이전 댓글 표시
I have the following loop:
sz = 20;
m = zeros(size(sz));
for i = 1:sz
[max_val, position] = max(sh(:)); %Find Max Position
m(i,:) = [position]; %Store Max Position in "m"
sh(sh==max(sh)) = []; %Delete current max value of "sh"
end
What I am trying to do here is pretty much to extract to "m" the top 20 highest values in "sh" (sh is a 3D matrix - 100x60x95). What i am not sure is happening is if " sh(sh==max(sh)) = []; " deletes only the value or the entire "row" containig the data of the max value.
What i ultimately need is once i delete the value, i need to delete everything else pertaining to that value in the matrix so when values shift they keep their order. Thanks so much for the help!
댓글 수: 3
Let's do some tests with a smaller 3D matrix:
format('compact');
sh = [1 3;2 4]+reshape([0 4 8],[1 1 3]);
display(sh);
[max_val, position] = max(sh(:)); %Find Max Position
display(max_val);
display(position);
sh(sh==max(sh)) = [];
display(sh);
Maybe that's not what we expected. Let's investigate what's going on there:
sh = [1 3;2 4]+reshape([0 4 8],[1 1 3]); % restore the original value of sh
display(max(sh));
So max(sh) gives more than one max value. That is because max() by default does the maximum along the first non-singleton dimension (i.e., the first dimension whose size is not 1), which in this case is dimension 1, so max(sh) gives us the maximum of each column. That's not what we want. We want the maximum of the whole thing, so we can do max(sh(:)) instead, which makes sh into a column vector before doing the max() operation (like you have in a previous line), and see what that does:
sh = [1 3;2 4]+reshape([0 4 8],[1 1 3]); % restore the original value of sh
display(max(sh(:)));
That's the maximum we expected. Let's delete it (actually this will delete any element of sh that equals its maximum value (i.e., 12), of which there is just one in this case, but when we try the 100x60x95 matrix, we'll have to account for the situation where the maximum occurs more than once in the matrix):
sh(sh==max(sh(:))) = [];
display(sh);
Now the maximum value is gone, but sh is a row vector with 11 elements. This is because deleting an element out of a matrix (even a 2D matrix) is an ambiguous situation: how should the remaining elements be shifted to try to preserve a matrix structure? Can they even be shifted to preserve a matrix structure? As opposed to, say, deleting a row (or column) from a 2D matrix, in which case it is clear that the remaining rows can be shifted up (or the remaining columns can be shifted left). (In this case, clearly, with 11 elements remaining, there is no 2D or higher-dimensional matrix than can be made with 11 elements, as 11 is prime.) So MATLAB just deletes the element(s) and returns a row vector of the element(s) that remain.
These questions that arise when deleting individual elements from matrices have to be answered by you, the programmer. If you had a 2D matrix and you want to delete one element from it, do you want to keep every other element and deal with the row vector MATLAB gives you? Or do you instead want to delete the entire row where the element is? Or the entire column? Or both? With a 3D matrix, there are even more possibilities: delete the entire x-y slice? the entire y-z slice? the entire x-z slice? both the x-y and x-z slices? both the x-y and y-z slices? both the x-z and y-z slices? all three x-y, y-z and x-z slices? In other words, in which dimension(s) should the the size of the matrix be reduced by one when the element is deleted?
You allude to this type of consideration in your question: "i need to delete everything else pertaining to that value in the matrix". But it is not clear at all (to me anyway) how to interpret this statement and get a set of dimensions in which 2D slices need to be removed from the 3D matrix, so that I could hope to write down actual MATLAB commands to do what you have in mind.
And more questions arise when thinking about deleting not just the single maximum value but the 20 highest values in a 3D matrix. If you want to delete a slice in each of the 3 dimensions each time, say, that's going to delete a bunch of other elements. What if some of those were in the top 20? Should they count towards the top 20 to be deleted or should the top 19 maximum values among what's left of the matrix be deleted next (which is the way your code is written now)? (And what if the maximum element occurs twice? Does that count as 2 of the top 20 or only 1?)
Anyway, sorry for the long lecture, but answering some of these questions about exactly what you want the code to do will help you get an answer on how you can go about doing it.
IDN
2021년 12월 23일
IDN
2021년 12월 24일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!