Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
search and replace values in matrix

Subject: search and replace values in matrix

From: doug kopet

Date: 19 Aug, 2010 03:53:06

Message: 1 of 8

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

Subject: search and replace values in matrix

From: Roger Stafford

Date: 19 Aug, 2010 07:10:20

Message: 2 of 8

"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

Subject: search and replace values in matrix

From: doug kopet

Date: 19 Aug, 2010 15:56:08

Message: 3 of 8

Roger:

I am attempting to achieve exactly that of propagating smaller values in A given that they are the same in B. I'm trying to create a searching algorithm for my supervisor at work.

You are definitely right, the indexing puzzles me . If you can I need some help on
how can I modify the indexing such the I direct the search to a NW,NE SE manner.

You also said right to left search may give different answer, but it doesnt seem so. Below is my code for it. Please have a look.
   

%===============================================
clc;
clear all;


 B = [ 1 1 ;
          1 1 ]


A= [2 9 ;
       6 4 ]

  [rs cs] =size(A)

   for i = 1:rs
   for j = cs:-1:2
     
        
      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 for helping

doug

Subject: search and replace values in matrix

From: doug kopet

Date: 19 Aug, 2010 16:04:25

Message: 4 of 8

Correction :

need help on searching in NW,NE and SW manner.

dougy

Subject: search and replace values in matrix

From: Roger Stafford

Date: 19 Aug, 2010 18:19:23

Message: 5 of 8

"doug kopet" <onedougk@gmail.com> wrote in message <i4jk6o$dm$1@fred.mathworks.com>...
> Roger:
>
> I am attempting to achieve exactly that of propagating smaller values in A given that they are the same in B. I'm trying to create a searching algorithm for my supervisor at work.
>
> You are definitely right, the indexing puzzles me . If you can I need some help on
> how can I modify the indexing such the I direct the search to a NW,NE SE manner.
>
> You also said right to left search may give different answer, but it doesnt seem so. Below is my code for it. Please have a look.
>
>
> %===============================================
> clc;
> clear all;
>
>
> B = [ 1 1 ;
> 1 1 ]
>
>
> A= [2 9 ;
> 6 4 ]
>
> [rs cs] =size(A)
>
> for i = 1:rs
> for j = cs:-1:2
>
>
> 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 for helping
>
> doug
- - - - - - - - - - -
  The code looks all right but you haven't tried it on the appropriate examples. To get a different result right-to-left than left-to-right try

A = [1 2 3]; B = [1 1 1];

The left-to-right gives A = [1 1 1] and right-to-left gives A = [1 1 2].

  As for other diagonal directions - well all right - I'll do SW also and leave the other two up to you. You should be able to figure out them out. Here's SW:

[rs,cs] = size(A);
for k = 2-cs:rs-2
  for i = max(k+1,1):min(k+cs-1,rs-1)
    j = k-i+cs+1;
    % Then you work with pairs i,j and i+1,j-1 (SW) to do your stuff
  end
end

Roger Stafford

Subject: search and replace values in matrix

From: doug kopet

Date: 19 Aug, 2010 22:19:21

Message: 6 of 8

Roger:

thanks again. Ive been at it for hours now and still cant figure it out. Im trying the NW search .I think changing the 'i' index can affect it. I know the search has to run for:

i,j= 3,3
i,j=3,2

i.j=2,3
i,j = 2,2

When I set i = 3:3 it does not change. Cant figure out why. Kind off frustrating.

Can you send a hint my way?

doug


%=================================================
  [rs,cs] = size(A);

for k = 2-cs:rs-2
  for i = 3:3
    j = i;
k
    i
    j
    if B(i,j)==B(i-1,j-1) && A(i,j)~=A(i-1,j-1)
     
        flag = true
        
       L = A(i,j)
       K = A(i-1,j-1)
        
if (A(i,j) < A(i-1,j-1))
        A(i-1,j-1) = A(i,j);
    
      else
       A(i,j) = A(i-1,j-1);

end
     end
  end
end
%=================================================

Subject: search and replace values in matrix

From: Roger Stafford

Date: 20 Aug, 2010 00:03:10

Message: 7 of 8

"doug kopet" <onedougk@gmail.com> wrote in message <i4kal8$ivo$1@fred.mathworks.com>...
> Roger:
> thanks again. Ive been at it for hours now and still cant figure it out. Im trying the NW search . ......
> ......
> Can you send a hint my way?
> doug
- - - - - - - - -
  Here ere are the other two directions, Doug:

%-----------
% Moving NW (Both i and j descend)
[rs cs] =size(A)
for k = 2-cs:rs-2
  for i = min(k+cs,rs):-1:max(k+2,2)
    j = i-k;
    % Work with pairs i,j and i-1,j-1
  end
end
%-----------
% Moving NE (i descends, j ascends)
[rs cs] =size(A)
for k = 2-cs:rs-2
 for i = min(k+cs,rs):-1:max(k+2,2)
  j = k-i+cs+1;
  % Work with pairs i,j and i-1,j+1
 end
end
A
%-----------

Roger Stafford

Subject: search and replace values in matrix

From: doug kopet

Date: 20 Aug, 2010 20:19:05

Message: 8 of 8

thank you roger

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us