### Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

## using find function and comparing results

BlkHoleSun

### BlkHoleSun (view profile)

さんによって質問されました 2017 年 10 月 21 日

### BlkHoleSun (view profile)

さんによって コメントされました 2017 年 10 月 22 日
Cedric Wannaz

### Cedric Wannaz (view profile)

さんの 回答が採用されました
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;

BlkHoleSun

### BlkHoleSun (view profile)

2017 年 10 月 21 日
I've been able to get a little further and complete the first question I had above. Here is my code so far...
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);
BlkHoleSun

### BlkHoleSun (view profile)

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 Wannaz

### Cedric Wannaz (view profile)

2017 年 10 月 22 日
I still highly advise you to understand the answer that I posted 2 hours ago.

サインイン to comment.

## 1 件の回答

2017 年 10 月 21 日

### Cedric Wannaz (view profile)

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

BlkHoleSun

### BlkHoleSun (view profile)

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!

サインイン to comment.

Translated by