リストの操作

Oct 01 2020

私はデータセット(衛星データ)を持っています-ここにリンクがあります:

https://drive.google.com/drive/folders/1qBOROSDHSRMykZjWgP7iRMtpQ3gBMceL?usp=sharing

データセットの特定の部分を読み取るには、次を使用します。

LISTA = FileNames[]; (* Here you assume you have your files in one specific Directory and list them*)

data1 = Import[
   LISTA[[1]], {"Datasets", "/PRODUCT/methane_mixing_ratio"}];

取得したデータ構造を確認する

Dimensions[data1]

{1、4173、215}

背景(またはNaNデータ)の一部を取り除き、0(ゼロ)を入れたい

Newdata1 = Flatten[ReplaceAll[data1, s_ /; s > 5000 -> 0]]

ただし、これを行うと、データ構造が「破棄」され、1つの要素リストのみが大きくなります。

Dimensions[Newdata1]

{897195}

それらの番号を置き換えてその構造(1 x 4173 x 214)を維持する方法はありますか?

Flattenオプションを使用せずに試してみましたが、機能しません。ここでいくつかの質問が表示され、ネストされたリストの問題の場合がありますが、解決方法がわかりません。

回答

1 user1066 Oct 01 2020 at 02:03

Clip?の場合

Clip[RandomReal[10000, {3, 4, 5}],{5000,Infinity},{0,Infinity}]
1 kglr Oct 31 2020 at 02:20

を使用することもできますThreshold。これは、驚いたことに、よりも高速ですClip。

SeedRandom[1]
data1 = RandomReal[10000, {1, 4173, 215}];

5000より大きい数値を0に置き換える:

t1a = First @ RepeatedTiming[
    res1a = Subtract[data1, Threshold[data1, 5000 + $MachineEpsilon]]];
t2a = First @ RepeatedTiming[res2a = Clip[data1, {0, 5000}, {0, 0}]];
t3a = First @ RepeatedTiming[res3a = data1 UnitStep[Subtract[5000, data1]]];
t4a = First @ RepeatedTiming[res4a = Replace[data1, s_ /; s > 5000 -> 0., All]];
t5a = First @ RepeatedTiming[res5a = ReplaceAll[data1, s_ /; s > 5000 -> 0.]];

res1a == res2a == res3a == res4a == res5a
True
Dimensions @ res1a
 {1, 4173, 215}

5000未満の数値を0に置き換える:

t1b = First @ RepeatedTiming[res1b = Threshold[data1, 5000 + $MachineEpsilon]];
t2b = First @ RepeatedTiming[res2b = Clip[data1, {5000, ∞}, {0, ∞}]];
t3b = First @ RepeatedTiming[res3b = data1 UnitStep[Subtract[data1, 5000]]];
t4b = First @ RepeatedTiming[res4b = Replace[data1, s_ /; s < 5000 -> 0., All]];
t5b = First @ RepeatedTiming[res5b = ReplaceAll[data1, s_ /; s < 5000 -> 0.]];

res1b == res2b == res3b == res4b == res5b
True
Dimensions @ res1b
 {1, 4173, 215}

タイミング比較

{timingsa, timingsb} = {{t1a, t2a, t3a, t4a, t5a}, {t1b, t2b, t3b, t4b, t5b}};

functionsa = {"Subtract[data1,Threshold[data1,5000+$MachineEpsilon]]", "Clip[data1,{0,5000},{0,0}]", "data1 UnitStep[Subtract[5000,data1]]", "Replace[data1,s_/;s>5000\[Rule]0., All]", "ReplaceAll[data1,s_/;s>5000\[Rule]0.]"}; functionsb = {"Threshold[data1,5000+$MachineEpsilon]", 
   "Clip[data1,{5000, Infinity},{0,Infinity}]", 
   "data1 UnitStep[Subtract[data1,5000]]", 
   "Replace[data1,s_/;s<5000\[Rule]0., All]", 
   "ReplaceAll[data1,s_/;s<5000\[Rule]0.]"};

Grid[Join[{{Item["Replace numbers greater than 5000 with 0", 
     Background -> LightBlue], SpanFromLeft}}, 
  Transpose[{functionsa, timingsa}], 
 {{Item["Replace numbers less than 5000 with 0", 
     Background -> LightBlue], SpanFromLeft}}, 
  Transpose[{functionsb, timingsb}]], 
 Alignment -> {{Left, "."}, Center}, Dividers -> All]