Why do I get "Array indices must be positive integers or logical values" error?
3 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Walter Roberson
on 20 Feb 2023
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.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!