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

조회 수: 1 (최근 30일)
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

채택된 답변

ADragon
ADragon 2018년 9월 6일
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
Rebecca Ellis
Rebecca Ellis 2018년 9월 7일
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 2018년 9월 7일
편집: ADragon 2018년 9월 7일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by