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

1 次查看(过去 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 个)

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 中查找有关 Logical 的更多信息

标签

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by