Calculation between different columns

Hello,
I am doing a MATLAB program that calculates the wind shear exponent of different heights. I have a matrix of size 2 by 12 that shows the dates and the wind speeds (WS) at a particular height. The titles of the different columns are shown below. In the second row (not shown) there is the date and the wind speed values.
Date WS@200m WS@150m WS@100m WS@80m WS@60m ...
WS@50m WS@40m WS@30m WS@38m WS@20m WS@10m
Now what I want to do is to find the wind shear exponent (alpha) using the following formula:
alpha=log(u1/u2)/log (h1/h2)
where the u stands for wind speed and h stands for height.
And I have to do this for all different combinations of heights, that is, the 200m with 150m, 200m with 100m, 200m with 80m .... and so on, the last one being 20m with 10m.
I am a beginner in MATLAB and am having trouble doing this. If someone could point me in the right direction by telling me which commands to use, it would be appreciated.

 Accepted Answer

This is relatively straightforward with the bsxfun function. Here, there are two calls to it, one for the altitude and one for the wind velocities. Each creates a matrix of its arguments, so that the structure of ‘u_ratios’ for example is:
u(1)/u(1) u(2)/u(1) ...
u(1)/u(2) u(2)/u(2)
u(1)/u(3) u(2)/u(3)
: .
: .
with the corresponding structure for ‘h_ratios’. The ‘alpha’ calculation then dies element-wise division of these the log of each matrix, producing the output. Note: the diagonals of ‘alpha’ are NaN because they are the results of log(1)/log(1) = 0/0 = NaN. So you may want to eliminate the NaN values on the main diagonal of ‘alpha_mtx_nan’. I did this by creating ‘alpha_mtx’ with -1 on the main diagnonal instead. The NaN values will cause problems with the calculations, so use either matrix. I opted not to ‘collapse’ the matrix by eliminating the main diagonal.
The Code:
u = sort(randi(25, 1, 11), 'descend'); % Create Wind Velocities (m/s)
h = [200 150 100 80 60 50 40 30 38 20 10]; % Altitude (m)
u_ratios = bsxfun(@rdivide, u, u'); % Divide ‘Rows’ Of ‘u’ By Columns Of ‘u'’
h_ratios = bsxfun(@rdivide, h, h'); % Divide ‘Rows’ Of ‘h’ By Columns Of ‘h'’
alpha_mtx_nan = log(u_ratios) ./ log(h_ratios);
alpha_mtx = triu(alpha_mtx_nan,1) + tril(alpha_mtx_nan,-1) + diag(-ones(1,size(alpha_mtx_nan,1),1));

4 Comments

Many thanks for your help Star Strider. One question though: the matrix u_ratios includes both u2/u1 (in R1C2) as well as u1/u2 (R2C1). I only require one of these ratios... so for the matrix alpha_mtx I need either those values below the diagonal value (-1) or those above.
Is there a way I can do this?
Again, many thanks for your help.
My pleasure.
In that instance, ‘alpha_mtx’ becomes simply:
alpha_mtx = triu(alpha_mtx_nan,1); % Upper Triangular Section — First Column Are All Zeros
alpha_mtx = alpha_mtx(:,2:end); % Eliminate First Column
The lower triangular portion are all zeros.
That should be what you want. The remaining components of the matrix remain as I described them initially.
Thank you for your help.

Sign in to comment.

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!