using find function and comparing results

1 Ansicht (letzte 30 Tage)
BlkHoleSun
BlkHoleSun am 21 Okt. 2017
Kommentiert: BlkHoleSun am 22 Okt. 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;
  3 Kommentare
BlkHoleSun
BlkHoleSun am 21 Okt. 2017
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 am 22 Okt. 2017
I still highly advise you to understand the answer that I posted 2 hours ago.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Cedric
Cedric am 21 Okt. 2017
Bearbeitet: Cedric am 21 Okt. 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
  1 Kommentar
BlkHoleSun
BlkHoleSun am 22 Okt. 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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Preprocessing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by