Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: search and replace values in matrix
Date: Thu, 19 Aug 2010 07:10:20 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 70
Message-ID: <i4ilcs$208$1@fred.mathworks.com>
References: <i4i9r2$cpm$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1282201820 2056 172.30.248.38 (19 Aug 2010 07:10:20 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 19 Aug 2010 07:10:20 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:663276

"doug kopet" <onedougk@gmail.com> wrote in message <i4i9r2$cpm$1@fred.mathworks.com>...
> Hello guys,
> would like to ask for some help.
> 
> below is my code. Basically there are 2 matrices ,A and B. In my code, I search matrix A from left to right . Basically,The search looks for if A has different values from its neighbouring value and if they are different, check B to see if the neighbouring values have same labeled value, and if so, then replace the the larger of the value in A with its neighbouring smaller value. So basically it is a type of propagation.
> 
> My code below works fine when i search from left to right of A.
> 
> My problem is that I want to write a similar block of code but this time I want to search in  in a southeast diagonal manner such that the answer I get is:
> 
> A= [2  9  ; 
>        6   2   ]
> 
> All labels in B are true for the statement " if    (B(i,j) == B(i, j + 1) && A(i,j) ~= A(i, j + 1))" , so basically my problem is doing a diagonal search.
> 
> 
> %==================================================
> clc;
> clear all;
> 
> 
>  B = [ 1   1   ;
>           1   1     ]
> 
> 
> A= [2  9  ; 
>        6   4   ]
> 
> [rs cs] =size(A)
> 
>    for i = 1:rs
>    for j = 1:(cs - 1)
>      
>         
>       if    (B(i,j) == B(i, j + 1) && A(i,j) ~= A(i, j + 1))  % they are checked to
>                                                                              % see if they have the same B label and different A labels
> 
>         flag = true
>         
> if (A(i,j) < A(i , j+1))
>         A(i , j+1) = A(i,j);
>     
>       else
>        A(i,j) = A(i , j+1);
> 
> end
>       end
>     end
>   end
>   
>   A
> %=================================================
> thanks doug
- - - - - - -
  As I understand your request, you want the given "propagation" in your for-loops to change from moving left-to-right to moving in a "southeast" diagonal direction.  I interpret "southeast" to mean moving diagonally with both i and j indices increasing together.  You should realize that this will make the for-loop indexing somewhat more complicated than before.  The following should accomplish this:

[rs,cs] = size(A);
for k = 2-cs:rs-2
  for i = max(k+1,1):min(k+cs-1,rs-1)
    j = i-k;
    if B(i,j)==B(i+1,j+1) && A(i,j)~=A(i+1,j+1)
     % etc., do your thing for i,j and i+1,j+1 pairs
  end
end

  The propagation you are using seems a curious process.  Moving from right to left could produce a different result from moving left to right, or in the diagonal method here, moving diagonally "southeast" would differ from moving "northwest".  What overall goal are you attempting to achieve?

  Note: It is not a good practice to use the letters 'i' and 'j' for indices as above because of possible confusion with their matlab definitions of being the square root of minus one for complex number calculations.  I have used them here so as not to introduce any more changes in notation than are necessary.

Roger Stafford