2

I am getting this error when trying to subtract the array with its mean value in Google Earth Engine JavaScript API

var from = ee.Array([2,3])
var meann = from.reduce(ee.Reducer.mean(),[0])
var to = ee.List.repeat(meann, from.length().get([0]))
var subtraction = from.subtract(to)

print(subtraction)
4
  • can you make the error reproducable?
    – Jobbo90
    Commented May 2, 2022 at 12:25
  • Do you have the solution for this error? Commented May 2, 2022 at 12:32
  • to is a list, from an array, hence the error
    – aldo_tapia
    Commented May 2, 2022 at 13:38
  • I fixed your error in my answer.
    – xunilk
    Commented May 2, 2022 at 13:59

1 Answer 1

3

Error is produced because to is a list of lists; not a single list. You can fix this with the function in following code.

var from = ee.Array([2,3]);
var meann = from.reduce(ee.Reducer.mean(),[0]);

print(meann);

var to = ee.List.repeat(meann, from.length().get([0]));

print(to);

var to = to.map(function (ele) {
  
  return ee.Array(ele).get([0]);
  
});

print(to);

var subtraction = from.subtract(to);

print(subtraction);

After running it in GEE code editor, code runs without any issues; as it can be observed in following picture:

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.