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:
Code Loops Like goto

Subject: Code Loops Like goto

From: Eric Williams

Date: 26 Feb, 2009 14:41:02

Message: 1 of 1

Hi, inside a function I have the following block of code. It is OK to assume that everything is declared and has a good value.

[xi, yi] = meshgrid(x,y)%;
first_col = (num_width+1):2*num_width:num_width*num_height%;
shiftme = [first_col' ones(length(first_col),num_width-1)]%;
shiftme = unique(cumsum(shiftme,2))%;
xi(floor(shiftme/num_width),mod(shiftme,num_width)) = xi(1,shiftme) - space/2%;

When I debug and step through, the code executes correctly until it gets to the last line, at which point <<it jumps back up to the first line>>. What am I doing wrong here? There is more code that executes normally above this block.

This block is simply taking a matrix of x co-ordinates that look like the following:

xi =

    0.0500 0.2000 0.3500 0.5000 0.6500 0.8000 0.9500
    0.0500 0.2000 0.3500 0.5000 0.6500 0.8000 0.9500
    0.0500 0.2000 0.3500 0.5000 0.6500 0.8000 0.9500
    0.0500 0.2000 0.3500 0.5000 0.6500 0.8000 0.9500
    0.0500 0.2000 0.3500 0.5000 0.6500 0.8000 0.9500
    0.0500 0.2000 0.3500 0.5000 0.6500 0.8000 0.9500
    0.0500 0.2000 0.3500 0.5000 0.6500 0.8000 0.9500

and "shifting" (subtracting a small number from each of the values of) every other row. When paired with non-shifted but similar y co-ordinate data, this creates a nice checkerboard pattern. The code above is pretty ugly, I realize, but I'd prefer not to do this in a loop. Does anyone see another <vectorized> way of doing this that I missed?

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