Maximum of Multidimensional Array using Flag Matrix

조회 수: 4 (최근 30일)
Everett625
Everett625 2017년 2월 22일
댓글: Stephen23 2017년 2월 25일
Howdy all,
I have a question that should be relatively straight forward, but is giving me a world of trouble. I have two identically sized matrices: one with a range of values and a second matrix that will serve to identify subsets within the first matrix. I have broken down my larger matrices into a fundamental process I'd like to complete, namely: find the maximum of the first matrix only if the second matrix has a zero (no identifier) at that position.
The following simple script should allow for the maximum to be identified, and then I set the identifier matrix to one to eliminate it from the next maximum calculation:
A = magic(7);
B = zeros(7, 7);
Maximum = 49;
while (Maximum > 25)
[Maximum, Index] = max(A(B == 0));
[I, J] = ind2sub(size(A(B==0)), Index);
B(I,J) = 1;
disp([Data, I, J])
end
which works for the first few iterations (Maximum = 49-46), but then produces the wrong index and then removes 1 from the index each additional iteration until reaching a constant value. I'm not sure why MATLAB is behaving like it is, but I would really appreciate any input on this problem. Thanks!
  댓글 수: 1
Stephen23
Stephen23 2017년 2월 25일
@Everett625: your code throws errors: first undefined Data, and then when I comment out that line some problem with matrix sizes. Please edit your question and provide a working example if you want help.

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

답변 (1개)

Matt J
Matt J 2017년 2월 22일
편집: Matt J 2017년 2월 22일
Are you sure you don't want all the maxima excluded in each pass (in case it is non-unique)? What about as follows?
while (Maximum > 25)
Maximum = max(A(B == 0));
B(A==Maximum & B==0) = 1;
end
  댓글 수: 1
Everett625
Everett625 2017년 2월 25일
This solution is fine for determining the maximum and setting the flag matrix correctly, but it seems there is something wrong with ind2sub. If I try to find the indices using either my original code or your simpler setup, the ind2sub command returns the wrong i and j values. And I need those to pass into a subroutine that performs a flood fill in my A matrix and marks zones in the B matrix. Am I missing something with ind2sub?

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

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by