MATLAB: Simple MATLAB question …

iterationMATLABmatrix

ok i am writting a code in which i need to write :
U(1,1) ,U (2,2) ,U (0,3) etc
and then i am going to assign some value to these separately…
for this purpose i use 2 "for" loops as follows:
for a=0:n-1 for b=0:n-1 where n i given a number by the user in the code.. and then i use U(a,b) ,so that it may write the points itself in each iteration….
but using this method matlab shows error that "U" is not defined function… am I writting it in correct way? if not then please help and correct it …
thanx.

Best Answer

MATLAB indexing starts at 1, not 0.
n = 5;
U = zeros(n); % Pre-allocate the memory.
for a = 1:n
for b = 1:n
U(a,b) = a+b;
end
end