목록 조작
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 (0)을 넣고 싶습니다.
Newdata1 = Flatten[ReplaceAll[data1, s_ /; s > 5000 -> 0]]
그러나 이것이 완료되면 데이터 구조가 "파괴"되고 하나의 큰 요소 목록 만됩니다.
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]