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

Status
Not open for further replies.

#### Old Nick

##### Advanced Member level 1 Hi,

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

sum(sum(phaseplot(30:60,35:65)))/(60-30)*(65-35)

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?

#### echo47

##### Advanced Member level 5 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
Status
Not open for further replies.