Negative number in Matrix or for loop or vector

I have two questions.
first question.
I wrote the this CODE 1.
----------- CODE 1 -------------
x=zeros(25,2);
for t= -1 : 4
for k = -1 : 4
x(t,:)=[t,k]
end
end
------------------------------
but ! I'm not do that. because of error.
Error : ??? Subscript indices must either be real positive integers or logicals.
I want " x(t,:) = [t,k] " to get the negative number.
or !! Second question .
I wrote this CODE 2 .
------------CODE 2----------
x=zeros(25,2);
for t= 1 : 4
for k = 1 : 4
x(t,:)=[t,k]
end
end
----------------------------
And I get the result. that is the "Attach a file"
please see the "Attach a file"
so , I want each value to get the dependent or individual value. for EX) :
x =
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
...............................
4 3
4 4
please help me resolve the this problem. Thanks for your help.

 채택된 답변

Roger Stafford
Roger Stafford 2015년 4월 20일

1 개 추천

For the first question, you need to distinguish between index values and the values stored at those locations. The former must be positive integers but the latter have no such limitation. In your case you can avoid that difficulty by changing the line
x(t,:)=[t,k]
to
x(t+2,:) = [t,k];
Or you can write
for t = 1:6
for k = 1:6
x(t,:) = [t-2,k-2];
end
end
But finally it would be much easier to avoid the for-loop altogether and write
[T,K] = meshgrid(-1:4);
x = [T(:),K(:)];
I don't understand your second question. Please explain further.

댓글 수: 2

Eun-ho Choi
Eun-ho Choi 2015년 4월 20일
편집: Eun-ho Choi 2015년 4월 20일
thanks you !!
Second question is about the "for loop"
I do not want the Result in "Attach file"
but ! I want small " for loop " to have each value.
the values in result should be changed and show up every moment.
thanks for your help ^^
As usual, take the semicolon off the end of the line and it will report the variable to the command window.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 4월 20일

편집:

2015년 4월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by