Why do I get: "Error using sum Invalid option. Option must be 'double' " even though my variable is a double?

조회 수: 38 (최근 30일)
Hi,
I have this for loop:
n = numel(first_three);
test_first_three = {};
for dd = 1:n
test_first_three{dd} = sqrt(sum(first_three{dd}(:,1:3),1,1).^2);
end
Whenever I try to run it I get the error,
Error using sum
Invalid option. Option must be 'double', 'native', 'default', 'omitnan', or 'includenan'.
It isn't clear to me why that is happening since my cell of doubles array is a double.
What I am trying to achieve with this loop is to apply this function: dist = sqrt( dx.^2 + dy.^2 + dz.^2 ); to every row and its adjacent row.
Here dx, dy and dz are the differences (subtracted from one another) between one row (ex: row 2) and its adjacent row (ex: row 1). That way row 2 - row 1, row 3 - row 2, row 4 - row 3 ... and so on.
Does anyone know how I can solve this?
  댓글 수: 2
yilak alemu
yilak alemu 2022년 4월 1일
Error using sum
Invalid option. Option must be 'double', 'native', 'default', 'omitnan' or 'includenan'.
how to correct

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2022년 3월 25일
At the moment you have code which is effectively
sum(x,1,1)
where x happens to have 3 columns.
The first 1 there would give the dimension to sum over (so, sum each column), but the second 1 there is in the slot where MATLAB expects only output class 'double', 'native', 'default', or else the flag about how to treat nans. No numeric value is expected in that third parameter.
  댓글 수: 4
lil brain
lil brain 2022년 3월 25일
Is there perhaps also a solution without using diff? Using diff seems to mess with later steps in my data analysis as I am not able to 2nd order derivative twice.
Walter Roberson
Walter Roberson 2022년 3월 25일
temp = first_three{dd}(:,1:3);
sqrt(sum( (temp(2:end,:) - temp(1:end-1,:)).^2, 2))
But you should expect exactly the same problem with secod order derivative later, as subtracting adjacent rows like this is exactly what diff() is going to do (but with a more efficient internal implementation.)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by