Negative number in Matrix or for loop or vector
3 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Roger Stafford
on 20 Apr 2015
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 Comments
Image Analyst
on 20 Apr 2015
As usual, take the semicolon off the end of the line and it will report the variable to the command window.
More Answers (0)
See Also
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!