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

1 view (last 30 days)
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

Accepted Answer

ADragon
ADragon on 6 Sep 2018
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.
AD
  6 Comments
Rebecca Ellis
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 ?
ADragon
ADragon on 7 Sep 2018
Edited: ADragon on 7 Sep 2018
OK. You can add an index to your vector.
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

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!