MATLAB Answers


Error message from matlab that I do not understand

Asked by Jianxiang
on 9 Jul 2013

I tried to understand the example from the book"computational surface and roundness metrology-chapter 12 page99-100" So i used their codes by typing into matlab to present a clear understanding for me. But there is a part of codes which caused the error message occurring. The codes I used were shown below and the highligheted codes may have some problem this caused the error message when I ran it:Error using + Matrix dimensions must agree. c(j-my,k-mx)=max(max(z1(j-my:j+my,k-mx:k+mx)+b)); Could any one try to have a look and tell me why this error occurring by even using the example codes from the book,please? And how to solve this error? Thank you very much

%3D sinusoidal surface dx=0.001; %spacing in x in mm dy=0.001; nx=128; %number of points along x ny=128; x=(0:1:nx-1)*dx; %generate x array y=(0:1:ny-1)*dy; for j=1:ny for i=1:nx z(j,i)=2*sin(2*pi*x(i)/0.064); end end z=z-mean(mean(z));%shift to 0 mean mesh(x,y,z)

%sphere ball generation radius=0.016; mx=radius/dx;my=radius/dy; j=1; for yr=-radius:dy:radius i=1; for xr=-radius:dx:radius if sqrt(xr^2+yr^2)<=radius b(j,i)=sqrt(radius^2-xr^2-yr^2); else b(j,i)=0; end i=i+1; end end

z1(ny+2*my,nx+2*mx+1)=0; z1(my+1:ny+my,mx+1:nx+mx)=z; for j=my+1:ny+my for k=mx+1:nx+mx c(j-my,k-mx)=max(max(z1(j-my:j+my,k-mx:k+mx)+b)); end end mesh(z); hold on; mesh(c);

  1 Comment

on 9 Jul 2013

Please edit your question to format the code to get rid of the line wrap...impossible to read otherwise.

Mash the 'help' button and follow instructions if it's not clear how to do this. Use the preview window to see your results and keep at it until they're clearly formatted w/ line-wrapping code.



No products are associated with this question.

1 Answer

Answer by Tom Lane
on 9 Jul 2013

I see j=1 in your code but I don't see j incremented after that. I suspect you intended to change j somewhere.

Here's a hint. Since this is a script, you can inspect the workspace variables after the error. When I do that I see you trying to add these two things of different sizes:

>> size(z1(j-my:j+my,k-mx:k+mx))
ans =
    33    33
>> size(b)
ans =
     1    33


Join the 15-year community celebration.

Play games and win prizes!

Learn more
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

MATLAB Academy

New to MATLAB?

Learn MATLAB today!