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