using find function and comparing results

4 views (last 30 days)
BlkHoleSun on 21 Oct 2017
Commented: BlkHoleSun on 22 Oct 2017
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;
Cedric Wannaz on 22 Oct 2017
I still highly advise you to understand the answer that I posted 2 hours ago.

Cedric Wannaz on 21 Oct 2017
Edited: Cedric Wannaz on 21 Oct 2017
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 on 22 Oct 2017
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!