Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How do I loop through an array and perform a check using Matlab?

Not open for further replies.

Old Nick

Advanced Member level 1
Sep 14, 2007
Reaction score
Trophy points
Activity points

I'm accessing part of an array and averaging it using the following command.


The array is the phase of a signal modulated light camera. the above code averages the block I want to look at well, however if the light is at a phase of 170 degrees a 11 deg reading error on a pixel can show up as -179 degrees.
So what I want to do is detect these wrap-arounds and either add or subtract 360 to the offending elements, then average again.

Currently my thinking is to average, then loop through the (area of interest of the) array and detecting anything that is greater than 180 deg. away from the averaged value (although the average value will be skewed in one direction if any of these are present).

So my first question is,
How do I loop through an array and perform this check, (I would use a for loop if I was programming in C, but I would guess there is an easier way of doing this in matlab.)

second question is,
can anyone think of a better way of correcting these wrap-arounds?

Re: Matlab question

Convert the phase angles to complex (quadrature) values, average the complex values, then convert back to phase.

phase = [178.3 175.1 -179.2 177.5 -174.7 176.3];
average_quadrature = mean(exp(j * phase * pi / 180));
average_phase = angle(average_quadrature) * 180 / pi;

Result: 178.8817

It may be better to start with complex data (each complex data point includes magnitude and phase information), and do all your processing with complex arithmetic. Then convert to magnitude/phase only when you are ready to present the results.

    Old Nick

    Points: 2
    Helpful Answer Positive Rating
Not open for further replies.

Part and Inventory Search

Welcome to