WHY THIS CODE IS SHOWING ERROR ?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Roger Stafford
le 20 Fév 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 commentaires
Shrirang
le 20 Fév 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 commentaire
Shrirang
le 20 Fév 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 .
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!