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.

 采纳的回答

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 个评论

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 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by