필터 지우기
필터 지우기

WHY THIS CODE IS SHOWING ERROR ?

조회 수: 1 (최근 30일)
SUDHANSHU SHARMA
SUDHANSHU SHARMA 2015년 2월 20일
댓글: Shrirang 2015년 2월 20일
fprintf('\nAddition of two matrix');
n=input('\nEnter the value of n in n*n matrix= ');
for i=1:n
for j=1:n
a(i,j)=input('\nEnter value of a(%d,%d)= ',i-1,j-1);
end
end
for i=1:n
for j=1:n
b(i,j)=input('\nEnter value of a(%d,%d)= ',i-1,j-1);
end
end
for i=1:n
for j=1:n
fprintf('\t%f',a(i,j)+b(i,j));
end
end

답변 (2개)

Roger Stafford
Roger Stafford 2015년 2월 20일
The usage of 'input' is invalid. It cannot accept more than two inputs. Moreover it is unable to print out the values of i-1 and j-1 in the spaces indicated by %d, as would be true for 'fprintf'. See the documentation at:
http://www.mathworks.com/help/matlab/ref/input.html
For example you would need to write:
a(i,j)=input(['Enter value of a(',num2str(i-1),',',num2str(j-1),')= ']);
rather than
a(i,j)=input('\nEnter value of a(%d,%d)= ',i-1,j-1);
Note: I don't understand why you have offset the index values by 1 here!

Shrirang
Shrirang 2015년 2월 20일
Hi just replace your input statement "input('\nEnter value of a(%d,%d)= ',i-1,j-1)" at line 5and 10 by following statement input(['\nEnter value of a(' num2Str(i-1) ' ' num2str(j-1) ')']) and your code will work.
fprintf('\nAddition of two matrix'); n=input('\nEnter the value of n in n*n matrix= '); for i=1:n for j=1:n a(i,j)=input(['\nEnter value of a(' num2Str(i-1) ' ' num2str(j-1) ')']); end end for i=1:n for j=1:n b(i,j)=input(['\nEnter value of a(' num2Str(i-1) ' ' num2str(j-1) ')']); end end for i=1:n for j=1:n fprintf('\t%f',a(i,j)+b(i,j)); end end
  댓글 수: 1
Shrirang
Shrirang 2015년 2월 20일
Sorry, Your main question was why this code is not working : It is because input function has one argument and it is just a string. You probably trying to apply syntax from "c" in m script.In m script syntax for displaying string on command window is slightly different as shown in above code. Hope this works for you .

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by