Why do I get "Array indices must be positive integers or logical values" error?

4 ビュー (過去 30 日間)
Milos
Milos 2023 年 2 月 20 日
コメント済み: Milos 2023 年 2 月 20 日
Sorry if this question is basic and the code is written badly. I am just starting out.
Why do I get "Array indices must be positive integers or logical values" error? My code is trying to tell me in which parts of the k by k by k grid on the unit square (actually torus) intersect squares of length d, with centres given by the collection of vectors u (i.e a 3 x m matrix). I'd like to get an output which is a k x k x k cell whose non - empty entries are the squares that intersect said cell
Here is my code;
function [G] = grid(u,d,k)
m = length(u);
K = zeros(3,15,length(u));
X = transpose(unique([(dec2bin(0:2^3-1)-'0');-(dec2bin(0:2^3-1)-'0')],'rows')) ;
Gi = cell(k,k,k);
h = zeros(m,1);
Z = cell(1,m);
%all possible combinations of standard basis multiplied by either 1 or -1
for i = 1:15
for j = 1:m
K(:,i,j) = floor(k.*mod((u(:,j) + (d./2).*X(:,i)),1));
end
end
for l = 1:m
Z{l} = [unique(K(:,:,l).','rows','stable')].';
end
for t = 1:m
h(t) = size(Z{t},2);
for n = 1:h(t)
Gi{Z{t}(1,n),Z{t}(2,n),Z{t}(3,n)} = cat(2,Gi{Z{t}(1,n),Z{t}(2,n),Z{t}(3,n)},t);
end
end
G = Gi;
end
Typically I set u = rand(3,10), k=10, d=0.01. The error I keep getting is "Index in position 1 is invalid. Array indices must be positive integers or logical values." I have tried emptying my workspace and checking my idices and still I can't seem to fix this. Furthermore, if I run line with the operator "cat" ,on its own, with a toy example it works.
Many Thanks for any info.

採用された回答

Walter Roberson
Walter Roberson 2023 年 2 月 20 日
K(:,i,j) = floor(k.*mod((u(:,j) + (d./2).*X(:,i)),1));
something mod 1 can give a result of 0. k times 0 will be 0. So K can contain entries that are zero.
Z{l} = [unique(K(:,:,l).','rows','stable')].';
If K contains zeros then unique(K) will contain zeros.
Gi{Z{t}(1,n),Z{t}(2,n),Z{t}(3,n)} = cat(2,Gi{Z{t}(1,n),Z{t}(2,n),Z{t}(3,n)},t);
You then use those zeros as indices.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by