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 Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by