How can one do this calculation?

4 ビュー (過去 30 日間)
alpedhuez
alpedhuez 2022 年 7 月 16 日
編集済み: dpb 2022 年 7 月 17 日
I have a data
Month City Temperature
1 1 25 24 22 19 ...
1 2 21 19 17 16 ...
2 1 23 19 18 17 ..
2 2 20 18 15 13 ..
Then what I like to do is to calculate, for each month, the nth highest temperature from temperature of every city of the month, and then add this info column for each row of this month.
In the above table, for n=2, the second highest temperature in Month 1 is 24, and 20 in Month 2. Thus the output one wants is
Month City 2nd_Hightst_Temp Temperatures
1 1 24 25 24 22 19 ...
1 2 24 21 19 17 16 ...
2 1 20 23 1918 17 ..
2 2 20 20 18 15 13 ..
Thank you in advance.
  2 件のコメント
Harshit Gupta
Harshit Gupta 2022 年 7 月 16 日
Could you provide a link/attachment of this data?
Image Analyst
Image Analyst 2022 年 7 月 16 日
We'll get to work on it as soon as you attach your data and code to read it in with the paperclip icon. But first read this:

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

採用された回答

dpb
dpb 2022 年 7 月 16 日
編集済み: dpb 2022 年 7 月 17 日
Assuming you put it into a table as @Steven Lord suggested, then
tHi2=maxk(tData.Temperatures,2,2); tHi2=tHi2(:,2); % get top two, keep second only
tData=addvars(tData,tHi2,'before','Temperatures','NewVariableNames',{'2nd Highest Temperature'});
ADDENDUM/CORRECTION:
The second optional argument to operate over the second dimension (by rows) was inadvertently omitted...
  2 件のコメント
alpedhuez
alpedhuez 2022 年 7 月 16 日
I was able to solve this issue on my end:
  1. for each month, extract temperatures
  2. reshape temperatures in a row vector
  3. run sort to reorder
  4. extract second highest
  5. add second highest value to the matrix
dpb
dpb 2022 年 7 月 17 日
編集済み: dpb 2022 年 7 月 17 日
See above comment/correction...moral -- read the doc if it isn't quite right!!! I just whiffed on forgetting to write it down when writing online...

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by