Azzera filtri
Azzera filtri

WHY THIS CODE IS SHOWING ERROR ?

1 visualizzazione (ultimi 30 giorni)
SUDHANSHU SHARMA
SUDHANSHU SHARMA il 20 Feb 2015
Commentato: Shrirang il 20 Feb 2015
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

Risposte (2)

Roger Stafford
Roger Stafford il 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!

Shrirang
Shrirang il 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 Commento
Shrirang
Shrirang il 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 .

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by