WHY THIS CODE IS SHOWING ERROR ?
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Roger Stafford
on 20 Feb 2015
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!
0 Comments
Shrirang
on 20 Feb 2015
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 Comment
Shrirang
on 20 Feb 2015
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 .
See Also
Categories
Find more on Introduction to Installation and Licensing 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!