Subscript indices must either be real positive integers or logicals

조회 수: 1 (최근 30일)
Hi everybody, I wrote this code but it's seems that it has some problems. Could you help me figure out my mistakes? Thank you!
A=[1,2,5,4,1,2,3,6; 1,4,5,6,0,0,0,0;2,3,2,5,6,0,0,0];
nsim=3;
M=6;
TRI=zeros(M,M,nsim);
k=1;
while(k<3)
m=1;
while(m<length(A(1,:)))
TRI(A(k,m),A(k,m+1),k)++;
m++;
end
k++;
end
  댓글 수: 1
Jan
Jan 2013년 6월 19일
Please be more specific. "It seems that is has sime problems" conatins too few information about the occurring problems.

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

채택된 답변

Kye Taylor
Kye Taylor 2013년 6월 18일
편집: Kye Taylor 2013년 6월 18일
There are several issues you need to resolve with your code before it will run. First, there is no increment operator in MATLAB, so expressions like
k++;
need to be replaced with
k = k+1;
Once you make those changes, you'll get an error about indexing... something like
Attempted to access TRI(6,0,2); index must be a positive integer or logical.
This is because the matrix A has zeros as entries, and you're using the entries of A to index into TRI. Since I don't know your end goal, I can't suggest with much certainty how to fix that error, but the code below will run. I've added comments to every line i changed. See if it produces what you're looking for and if not, respond with the issue...
A=[1,2,5,4,1,2,3,6; 1,4,5,6,0,0,0,0;2,3,2,5,6,0,0,0];
A = A + 1; % now indices are between 1 and 7 instead of 0 and 6
nsim=3;
M=7; % changed to 7 to make TRI big enough for indices in A
TRI=zeros(M,M,nsim);
k=1;
while(k<3)
m=1;
while(m<length(A(1,:)))
TRI(A(k,m),A(k,m+1),k) = TRI(A(k,m),A(k,m+1),k) + 1; % no increment op.
m=m+1; % no increment op.
end
k = k+1; % no increment op.
end
  댓글 수: 3
Kye Taylor
Kye Taylor 2013년 6월 18일
편집: Kye Taylor 2013년 6월 18일
When I use the code that I provided above. There are not zeros in TRI(:,:,2), so please provide your modified code (as formatted code so it is easy to copy/paste).
Also, what do you expect to be in TRI(:,:,2).. .you can describe this to me in words.
Igina
Igina 2013년 6월 19일
Hi, now I'm working on another computer and the code seems to go well! Thank you!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by