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:
Removing for loops from a code.

Subject: Removing for loops from a code.

From: xplore29

Date: 21 Mar, 2013 15:49:07

Message: 1 of 2

I have two masks A and C of finite dimensions.
Let A=[1 1;1 1];C=[1 1 1;1 1 1;1 1 1];
I want to code the number of shifts that A would require to completely scan C. In this example the #shift=4. But if C=[1 1 1;1 1 1;1 1 0]; #shifts=3 (A zero in C indicates that this index is not considered for further processing)

So far I have come up with a For-Loop code which counts the #shifts along a given row and then adds them up.(This code assumes explicitly that rB>rA, cB>cA)
This code gives me #shifts along each row.

A=[1 1;1 1]
    C=[1 2 0;4 5 0;0 0 0]
% C=[1 2 3;4 5 6;0 0 0]
% C=[1 2 3;4 5 6;7 0 0]
% C=[1 2 3;4 5 6;7 8 0]
% C=[1 2 3;4 5 6;7 8 9]
% C=[1 2 3;4 0 6;7 8 9]

[rA cA]=size(A);
[rC cC]=size(C);

for i=1:rC-rA+1
    shift(i)=0;
    for j=1:cC-cA+1
         C(i:i+rA-1,j:j+cA-1);
         indicator = find(C(i:i+rA-1,j:j+cA-1)==0);
        if isempty(indicator)==1
           shift(i)=shift(i)+1;
        end
    end
end

I am looking for an alternative without these for loops.
Any suggestions??
 

Subject: Removing for loops from a code.

From: dpb

Date: 21 Mar, 2013 19:30:11

Message: 2 of 2

On 3/21/2013 10:49 AM, xplore29 wrote:
> I have two masks A and C of finite dimensions.
> Let A=[1 1;1 1];C=[1 1 1;1 1 1;1 1 1];
> I want to code the number of shifts that A would require to completely
> scan C. In this example the #shift=4. But if C=[1 1 1;1 1 1;1 1 0];
> #shifts=3 (A zero in C indicates that this index is not considered for
> further processing)
>
> So far I have come up with a For-Loop code which counts the #shifts
> along a given row and then adds them up.(This code assumes explicitly
> that rB>rA, cB>cA) This code gives me #shifts along each row.
>
> A=[1 1;1 1]
> C=[1 2 0;4 5 0;0 0 0]
> % C=[1 2 3;4 5 6;0 0 0]
> % C=[1 2 3;4 5 6;7 0 0]
> % C=[1 2 3;4 5 6;7 8 0]
> % C=[1 2 3;4 5 6;7 8 9]
> % C=[1 2 3;4 0 6;7 8 9]
>
> [rA cA]=size(A);
> [rC cC]=size(C);
>
> for i=1:rC-rA+1
> shift(i)=0;
> for j=1:cC-cA+1
> C(i:i+rA-1,j:j+cA-1);
> indicator = find(C(i:i+rA-1,j:j+cA-1)==0);
> if isempty(indicator)==1
> shift(i)=shift(i)+1;
> end
> end
> end
>
> I am looking for an alternative without these for loops.
> Any suggestions??

I didn't try it, but isn't the result simply the size difference between
A and C reduced by any rows/columns containing a 0?

Well, let's see...

C=[1 1 1;
    1 1 1;
    1 1 0];
...

OK, no, not quite as that removes both last row and column...but, I'd
think along that line I believe.

Also it's a form of convolution it would seem. Unfortunately, I've got
to run at the moment...

--

Tags for 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