Array indices must positive or integers because I have decimals in mymatrixes

조회 수: 4 (최근 30일)
Tino
Tino 2019년 5월 30일
답변: Guillaume 2019년 5월 30일
Hello I have a one column data D which consist of double and decimal numbers and what to compute it using this code below
K = 1;
croppedz = D(1:end-mod(end, 2*K), :);
result = zeros(size(croppedz, 1) / 2 / K, size(croppedz, 2));
for col = 1:size(croppedz, 2)
result(:, col) = sum(reshape(sort(croppedz(1:2:end, col)), K, []), 1) ./ sum(reshape(sort(croppedz(2:2:end, col)), K, []), 1); %sort even/odd rows, reshape each into a Kx? matrix, sum across rows and divide the two sums
end
a = result;
I am getting the error
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in kmean (line 18)
result(:, col) = sum(reshape(sort(croppedz(1:2:end, col)), K, []), 1) ./ sum(reshape(sort(croppedz(2:2:end, col)), K, []), 1); %sort even/odd rows, reshape each into a Kx? matrix, sum across rows and divide the two sums
Can someone help me
Tino
  댓글 수: 1
Raj
Raj 2019년 5월 30일
편집: Raj 2019년 5월 30일
Debugging will be easy if you share your data because I ran the code with
D=rand(10,1)
and got no errors.

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

답변 (1개)

Guillaume
Guillaume 2019년 5월 30일
The actual indexing (of croppedz) in line 18 cannot produce an Array indices must be positive integers or logical values error. The only possible reason you can get this error is therefore if you've shadowed any of the functions used on that line by a variable, thereby changing a function call into an indexing of the variable with the same name.
My bet would be that you have a variable called sum. The other possible culprits would be sort or reshape.
Never call a variable sum (or mean, max, min, all, var, std, image, etc. the other commonly shadowed functions). You will inevitably run into this sort of problems.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by