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 ^^
Image Analyst
Image Analyst 2015 年 4 月 20 日
As usual, take the semicolon off the end of the line and it will report the variable to the command window.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops 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