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

4 views (last 30 days)
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 Comment
Raj
Raj on 30 May 2019
Edited: Raj on 30 May 2019
Debugging will be easy if you share your data because I ran the code with
D=rand(10,1)
and got no errors.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 30 May 2019
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.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!