using find function and comparing results

1 回表示 (過去 30 日間)
BlkHoleSun
BlkHoleSun 2017 年 10 月 21 日
コメント済み: BlkHoleSun 2017 年 10 月 22 日
I've been able to create a 3-D cell array with months and wind speeds in different locations. I've then calculated the averages in the perspective locations. Here's where things go over my head (all using the find function)...1, how many months is the wind speed below the average in each location? 2, How many months and which ones was the wind speed in the first location higher than the second? 3, How many months and which ones was the wind speed in first location within ±0.2mph of the second? How can I use the find function to compare results in different arrays? As always, thanks for teaching!
A(:, :, 1) = {'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec'};
A(:, :, 2) = {6.7 7.5 8.5 9.5 10.4 10.9 11.2 10.5 9.1 7.6 6.3 6.5};
A(:, :, 3) = {9.0 9.6 9.9 9.4 8.8 8.0 7.3 7.2 7.7 8.6 8.6 8.5};
B = sum([A{:,:,2}])/12;
C = sum([A{:,:,3}])/12;
  3 件のコメント
BlkHoleSun
BlkHoleSun 2017 年 10 月 21 日
I've continued to press on and solved question 3. Below is my code so far. Last question is: How many months and which ones was the wind speed in first location within ±0.2mph of the second?
A(:, :, 1) = {'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec'};
A(:, :, 2) = {6.7 7.5 8.5 9.5 10.4 10.9 11.2 10.5 9.1 7.6 6.3 6.5};
A(:, :, 3) = {9.0 9.6 9.9 9.4 8.8 8.0 7.3 7.2 7.7 8.6 8.6 8.5};
B = sum([A{:,:,2}])/12;
C = sum([A{:,:,3}])/12;
x=(B./[A{:,:,2}]);
x1=(x<1);
y=(C./[A{:,:,3}]);
y1=(y<1);
y2=sum(y1);
fprintf('there are %1.0f months below average in San Fransisco, \n', x2)
fprintf('and %1.0f months below average in Orlando. \n', y2)
[r,c]=find([A{:, :, 2}]>[A{:, :, 3}]);
L=A(:, c, 1);
M=sum(r);
fprintf('There are %1.0f months where San Fransisco has higher winds than Orlando \n', M)
fprintf('and those months are %1.3s, %1.3s, %1.3s, %1.3s, %1.3s, %1.3s. \n', L{1, :} )
Cedric
Cedric 2017 年 10 月 22 日
I still highly advise you to understand the answer that I posted 2 hours ago.

サインインしてコメントする。

採用された回答

Cedric
Cedric 2017 年 10 月 21 日
編集済み: Cedric 2017 年 10 月 21 日
Don't save regular numeric data in cell arrays, you cannot compute with them and you have to go through notation- and computation-heavy concatenations each time you need to perform some computation. Instead, create a cell array for the non-numeric or non-regular content, and a numeric array for the numeric content:
>> months = {'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec'} ;
>> data(1,:) = [6.7 7.5 8.5 9.5 10.4 10.9 11.2 10.5 9.1 7.6 6.3 6.5] ;
>> data(2,:) = [9.0 9.6 9.9 9.4 8.8 8.0 7.3 7.2 7.7 8.6 8.6 8.5] ;
Now data is a 2D array of all measures; one row per location, and 12 columns for 12 months. Then you can use functions like MEAN and SUM for computing what you need. Note that they take a second argument that defines the direction along which they must operate. Months are along dim 2, so:
>> mean( data, 2 )
ans =
8.7250
8.5500
gives you monthly means. Then you can apply some relational operation between the data and these means:
>> data > mean( data, 2 )
ans =
2×12 logical array
0 0 0 1 1 1 1 1 1 0 0 0
1 1 1 1 1 0 0 0 0 1 1 0
and even count the number of true (1) using a sum:
>> sum( data > mean( data, 2 ), 2 )
ans =
6
7
EDIT : Note that data>mean(data,2) hides an automatic expansion (a feature that is not supported by MATLAB versions < 2016b), because data is a nLoc x 12 array and the means are a nLoc x 1 column vector. The way to explicitly perform it is to use BSXFUN, which is not really the first thing that you should be learning in MATLAB. If the expansion is not supported, you can loop through months for example, or repeat the means to form an array that matches the size of data:
>> means = repelem( mean(data,2), 1, 12 ) ; % nLoc x 12, matches data size
>> sum( data > means, 2 )
ans =
6
7
  1 件のコメント
BlkHoleSun
BlkHoleSun 2017 年 10 月 22 日
This was very helpful! and you're right, organizing it in this was is much easier. I was definitely going about it the hard way!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by