# How to create a loop which nan means every datapoint of three components

3 views (last 30 days)
Rebecca Ellis on 6 Sep 2018
Edited: ADragon on 7 Sep 2018
I have 3 datasets at 11564597 data points each - my calculation is U = nanmean(sqrt((u).^2+(v).^2+(w).^2)); to get the mean flow of the 3 sinusoidal curves. To get an array that has 11564597 points to plot on top of the 3 components as the mean flow of the three compontents.
My code so far is:
U= NaN*ones(length(velocity_x_nortek.smooth));
for i = 1:length(velocity_x_nortek.smooth)
U(i)= nanmean(sqrt((velocity_x_nortek.smooth).^2+(velocity_y_nortek.smooth).^2+(velocity_z_nortek.smooth).^2);
end

Hi Rebecca, you do not need the for loop since the equation can operate on arrays. Just use:
U = nanmean(sqrt((velocity_x_nortek.smooth).^2+(velocity_y_nortek.smooth).^2+(velocity_z_nortek.smooth).^2);
plot([velocity_x_nortek.smooth velocity_y_nortek.smooth velocity_z_nortek.smooth U])
Assuming you have column vectors for plotting.

Your variables are column vectors already. Maybe I misunderstood your equation. To get the mean by row of velocity_x_nortek.smooth, velocity_y_nortek.smooth, and velocity_z_nortek.smooth, use:
U = nanmean([velocity_x_nortek.smooth velocity_y_nortek.smooth velocity_z_nortek.smooth],2)
Rebecca Ellis on 7 Sep 2018
The calculation is
U = nanmean(sqrt((u).^2+(v).^2+(w).^2))
is correct -
I just need to loop it that it does the calculation for every 11564597 points -
U = zeros(length(timevec), 1);
for i = 1:length(velocity_x_nortek.smooth)
U(i)= nanmean(sqrt((velocity_x_nortek.smooth).^2+(velocity_y_nortek.smooth).^2+(velocity_z_nortek.smooth).^2));
end
The loop I have doesn't work ?
for i = 1:length(velocity_x_nortek.smooth)
U(i)= nanmean(sqrt((velocity_x_nortek.smooth(i)).^2+(velocity_y_nortek.smooth(i)).^2+(velocity_z_nortek.smooth(i)).^2));
end
But you will be taking the mean of a scalar value. So I am confused as to what you are trying to do...
If you are trying to calculate RMS then switch mean and sqrt.
for i = 1:length(velocity_x_nortek.smooth)
U(i)= sqrt(nanmean((velocity_x_nortek.smooth(i)).^2+(velocity_y_nortek.smooth(i)).^2+(velocity_z_nortek.smooth(i)).^2));
end