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:
Help with Matlab Code

Subject: Help with Matlab Code

From: Mat_rac247 Parik

Date: 17 Dec, 2010 18:21:22

Message: 1 of 5

Zdata =
0 0 0 1 2 3 0 0 0
0 0 2 3 4 5 6 0 0
9 1 2 3 4 5 6 7 9
1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 0
0 0 2 3 4 5 6 0 0
0 0 0 1 2 3 0 0 0

I have a matrix which is 149X 132 which is similar to which I have shown.
I want to calculate the average of 4 non zero adjacent numbers for eg (i, j), (i+1,j), (i,j+1),(i+1,j+1).
I am realtively new to matlab as well as coding. Can somebody help me out. Currently I have a very long code for my matrix which I believe can be made robust but not sure how to proceed..Any help will be appreciated...Currently my code looks something like this.. where I am splitting according to the columns....

For Column 1 to 77
N =77; M= 149;y=1;
for j = 1:1:N
       for i = 1:1:M-1
if Zdata(i,j)~=0
    if Zdata(i+1,j)~=0
       AveHeight (y,z) = (Zdata(i,j)+ Zdata(i+1,j)+ Zdata (i,j+1) + Zdata (i+1,j+1))/4;
       y=y+1;
    end
end
      end
   i=1;
 end

For Columns 78 to 82
i = 1; j=1 ; N =82; M= 149;y=1;
for j = 78:1:N
       for i = 1:1:M-1
if Zdata(i,j)~=0
    if Zdata(i+1,j+1)~=0
       AveHeight1 (y,z) = (Zdata(i,j)+ Zdata(i+1,j)+ Zdata (i,j+1) + Zdata(i+1,j+1))/4;
       y=y+1;
    end
   end
       end
    i=1;
 end

FOr Columns 83 to 132
i = 1; j=1 ; N =83; M= 149;y=1;
for j = 132:-1:N
       for i = 1:1:M-1
if Zdata(i,j)~=0
    if Zdata(i+1,j)~=0
        %if Zdata (i,j+1)~=0
       AveHeight2(y,z) = (Zdata(i,j)+ Zdata(i+1,j)+ Zdata(i,j-1) + Zdata (i+1,j-1))/4;
       y=y+1;
  % end
    end
end
       %z= z+1;
       end
   i=1;
end

Any Help Will be Appreciated.

Thanks,

R

Subject: Help with Matlab Code

From: Sean de

Date: 17 Dec, 2010 18:44:08

Message: 2 of 5

"Mat_rac247 Parik" <rac2407@yahoo.co.in> wrote in message <ieg9n2$60j$1@fred.mathworks.com>...
> Zdata =
> 0 0 0 1 2 3 0 0 0
> 0 0 2 3 4 5 6 0 0
> 9 1 2 3 4 5 6 7 9
> 1 2 3 4 5 6 7 8 9
> 0 1 2 3 4 5 6 7 0
> 0 0 2 3 4 5 6 0 0
> 0 0 0 1 2 3 0 0 0
>
> I have a matrix which is 149X 132 which is similar to which I have shown.
> I want to calculate the average of 4 non zero adjacent numbers for eg (i, j), (i+1,j), (i,j+1),(i+1,j+1).

%Data
Zdata =[
0 0 0 1 2 3 0 0 0
0 0 2 3 4 5 6 0 0
9 1 2 3 4 5 6 7 9
1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 0
0 0 2 3 4 5 6 0 0
0 0 0 1 2 3 0 0 0];

%Engine
Znz = Zdata~=0; %not equal to zero
Zsum = conv2(Zdata,ones(2)); %Sum in Numerator
Znzn = conv2(double(Znz),ones(2)); %Number in denominator

Znz_mean = Zsum(2:end-1,2:end-1)./Znzn(2:end-1,2:end-1); %Calculation of applicable points
Znz_mean(isnan(Znz_mean)) = 0

Subject: Help with Matlab Code

From: Mat_rac247 Parik

Date: 17 Dec, 2010 22:59:06

Message: 3 of 5

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <iegb1o$31e$1@fred.mathworks.com>...
> "Mat_rac247 Parik" <rac2407@yahoo.co.in> wrote in message <ieg9n2$60j$1@fred.mathworks.com>...
> > Zdata =
> > 0 0 0 1 2 3 0 0 0
> > 0 0 2 3 4 5 6 0 0
> > 9 1 2 3 4 5 6 7 9
> > 1 2 3 4 5 6 7 8 9
> > 0 1 2 3 4 5 6 7 0
> > 0 0 2 3 4 5 6 0 0
> > 0 0 0 1 2 3 0 0 0
> >
> > I have a matrix which is 149X 132 which is similar to which I have shown.
> > I want to calculate the average of 4 non zero adjacent numbers for eg (i, j), (i+1,j), (i,j+1),(i+1,j+1).
>
> %Data
> Zdata =[
> 0 0 0 1 2 3 0 0 0
> 0 0 2 3 4 5 6 0 0
> 9 1 2 3 4 5 6 7 9
> 1 2 3 4 5 6 7 8 9
> 0 1 2 3 4 5 6 7 0
> 0 0 2 3 4 5 6 0 0
> 0 0 0 1 2 3 0 0 0];
>
> %Engine
> Znz = Zdata~=0; %not equal to zero
> Zsum = conv2(Zdata,ones(2)); %Sum in Numerator
> Znzn = conv2(double(Znz),ones(2)); %Number in denominator
>
> Znz_mean = Zsum(2:end-1,2:end-1)./Znzn(2:end-1,2:end-1); %Calculation of applicable points
> Znz_mean(isnan(Znz_mean)) = 0

Hey Sean,

THanks a lot for your help. But Somehow the code pads in some extra values at places where it should be ideally supposed to be zero. Ideally, if any of the ( i,j), (i,j+1),(i,j+1) or (i+1,j+1) is zero it should not calculate the average of the four and skip that position.

The inital data is something like this

Zdata =
[ 0 0 3.187
0 0 3.195
0 0 3.204
0 0 3.213
0 0 3.222
0 3.193 3.231
0 3.202 3.239
0 3.210 3.247
0 3.216 3.253
0 3.219 3.256
0 3.222 3.259
3.181 3.224 3.262
3.190 3.227 3.265
3.193 3.230 3.267]

Ideally in the first column for this example there should be only 3 values but I got the answer as follows:

0 3.191 3.210
0 3.200 3.219
0 3.209 3.227
0 3.219 3.236
3.193 3.215 3.245
3.198 3.216 3.253
3.206 3.224 3.261
3.213 3.232 3.269
3.217 3.236 3.273
3.220 3.239 3.276
3.211 3.242 3.279
3.207 3.244 3.282
3.210 3.247 3.285
3.213 3.250 3.288

IN this case there should be only the last 3 values that should be present and not the values above it for the first column. Similarly the averaging should start for column 2 such that the first 5 values are still zero.

Appreciate your inputs.

Thanks,

R

Subject: Help with Matlab Code

From: ImageAnalyst

Date: 18 Dec, 2010 00:22:48

Message: 4 of 5

Look again and I think you'll realize your mistake, and that Sean was
right. For row 5, it sees
0 0
0 3.193

If you're only looking at non-zero values, then only 3.193 gets
averaged, the average of which is 3.193, which is exactly what you got
in row 5, column 1. So the code is correct>

One might argue though that you should have only 2 columns because you
can only fit two 2x2 windows going across, one with the left column of
the window in column 1 of the larger matrix, and one with the left
column of the window at column 2 of the larger matrix, and a third 2x2
window cannot fit across. However this is just the old boundary
effects issue and there are several ways to deal with it depending on
what you want to have happen there.

Subject: Help with Matlab Code

From: Mat_rac247 Parik

Date: 18 Dec, 2010 00:51:19

Message: 5 of 5

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <2fe30e0a-6e17-4661-a31f-70ea5405d943@w17g2000yqh.googlegroups.com>...
> Look again and I think you'll realize your mistake, and that Sean was
> right. For row 5, it sees
> 0 0
> 0 3.193
>
> If you're only looking at non-zero values, then only 3.193 gets
> averaged, the average of which is 3.193, which is exactly what you got
> in row 5, column 1. So the code is correct>
>
> One might argue though that you should have only 2 columns because you
> can only fit two 2x2 windows going across, one with the left column of
> the window in column 1 of the larger matrix, and one with the left
> column of the window at column 2 of the larger matrix, and a third 2x2
> window cannot fit across. However this is just the old boundary
> effects issue and there are several ways to deal with it depending on
> what you want to have happen there.

YEs Indeed I overlooked that Sean was right!!!

What I would like to see is that it should start from Left hand side and move to every column on the right, so in that sense it is important to see first that only ( i,j) value is zero. If this is true, then it should check for other values ( i+1, j), ( i,j+1), ( i+1,j+1) and if either one of these is zero it should not average the numbers. I guess that is what i had in my original code but was too lengthy too understand. Hope any one can help me with this ?

Thanks a lot everyone for your help!!!

R

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