simple MATLAB question ...

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.

 채택된 답변

Matt Fig
Matt Fig 2011년 6월 12일

1 개 추천

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

댓글 수: 5

Ambreen
Ambreen 2011년 6월 12일
will you please explain the line "U(a,b)=a+b" ?
Matt Fig
Matt Fig 2011년 6월 12일
This assigns the value a+b to the matrix U at location (a,b). Run the code to see the result...
Ambreen
Ambreen 2011년 6월 12일
ok thanx,one more question ..you told indexing starts from 1,but what i should do to write U(0,1) ,U(0,2) etc????
Matt Fig
Matt Fig 2011년 6월 12일
You simply cannot index into a MATLAB array with 0. In other languages (C++ for example) you can index into an array with 0, but not MATLAB. This will error. Look:
A = [1 2 4 6 8 9];
A(1)
A(2)
A(3)
A(0) % Error...
Ambreen
Ambreen 2011년 6월 12일
ok thanx

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2011년 6월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by