How do I overcome the "Subscript Indices" problem?

Firstly, I know what it is and why it happens. I just want to know how to overcome the problem.
My code is as followed:
b=zeros(3,3,3); %returns an n-by-n matrix of zeros.
a=[0 1 1; 1 0 1; 1 1 0];
[r, c]=size(a);
for i = 1:r
b(a(i,c-2),a(i,c-1),a(i,c)) = 1;
end
[m, n, o]=size(b);
figure (1)
[x,y,z] = meshgrid(1:m,1:n,1:o);
scatter3(x(:),y(:),z(:),90,b(:),'filled')

댓글 수: 3

KSSV
KSSV 2016년 6월 9일
What exactly you are trying to do? The value of a(1,1) is 0 and it is called as index in b..so obviously there will be an error.
What is the aim of your statement
b(a(i,c-2),a(i,c-1),a(i,c)) = 1;
taking into account that some of those a values are 0?
Thomas
Thomas 2016년 6월 9일
I made this statement for a previous matrix that included no zeros

댓글을 달려면 로그인하십시오.

 채택된 답변

John BG
John BG 2016년 6월 9일

1 개 추천

Thomas
your are trying to index b with a null.
In MATLAB matrix indices have to be >0, and in your for loop you are trying to index b with elements of a, that some happen to be zero.
To index b you need 3 indices, that for instance could be
b(c-2,c-1,c) = 1;
This fixes the
'Subscript indices ..'
crash, yet since don't know the matrix you want to generate, cannot go anyfurther without knowing what do you really want in matrix b.
If you find this answer of any help solving your question,
please click on the thumbs-up vote link, or mark it as accepted
thanks in advance
John

댓글 수: 1

Thanks for the solution, it did fix the problem.
What I want to show are the three vectors [0 1 1] [1 0 1] [1 1 0] represented only by the spheres.
I did this successfully using vector arrows.
if true
% code
end
a = [0 1 1];
b = [1 0 1];
c = [1 1 0];
starts = zeros(3,3);
ends = [a;b;c];
quiver3(starts(:,1), starts(:,2), starts(:,3), ends(:,1), ends(:,2), ends(:,3))
axis equal
axis on
view(3), axis vis3d
camproj perspective, rotate3d on
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2016년 6월 9일

댓글:

2016년 6월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by