필터 지우기
필터 지우기

Looping these equations over multiple workspace variable names

조회 수: 1 (최근 30일)
Lewis Holden
Lewis Holden 2017년 2월 6일
편집: Stephen23 2017년 2월 6일
I have this code which calculates the mean value of all wind speeds in the 99th percentile. The trouble is I have wind data for 1949-1999 in the workspace (named nineteen_49 through to 99) I am struggling to make a loop which calls the name of the new variable each time and also creates new variables in place of x, y and j - can anybody help?:
% calculate value of 99th percentile value for wind spd
x = prctile(nineteen_49(:,6),99)
% create logical matrix that corresponsds to all wind speed values of great
% than 99th percentile (collecton o 0's and 1's where 1 denotes the
% location of each value great > x
y = nineteen_49(:,6) > x
% create a vector j that is the 6th column of 1949 (ie just speeds)
j = nineteen_49(:,6)
% calculate the mean of 99th percentile values
mean_99th_percentile_1949 = mean(j(y))
% calculate the frequency of events over

채택된 답변

Geoff Hayes
Geoff Hayes 2017년 2월 6일
Lewis - just use an numeric (or cell) array to store all your wind data for each year instead of having all of this data spread about many variables which would require you to do the above. See Stephen's answer at https://www.mathworks.com/matlabcentral/answers/105936-how-to-make-dynamic-variable-names-a1-a2-an-with-for-loop-using-eval-num2str which describes why it isn't a good idea to create dynamically named variables in MATLAB.
  댓글 수: 2
Lewis Holden
Lewis Holden 2017년 2월 6일
Thankyou Geoff.
I have created a cell array with all my data but I am still struggling to try and implement the code above to all the data because when I try to loop through the cell array this doesn't work, see below. Any ideas? Thanks for your help.:
mean_99th_percentiles = zeros(1,68)
for i = 1:68
mean_99th_percentile{i} = mean(prctile(my_cell{:,i}(:,6),99));
end
Stephen23
Stephen23 2017년 2월 6일
편집: Stephen23 2017년 2월 6일
N = 68;
mean99per = nan(1,N);
for k = 1:N
mean99per(k) = mean(...);
end
You were using the wrong kind of brackets. zeros creates a numeric array, not a cell array. Learn about cell arrays here:

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by