my fprintf statements keep repeting themself I just want it to be print one time

4 vues (au cours des 30 derniers jours)
Aminata camara
Aminata camara le 28 Août 2022
Commenté : Star Strider le 28 Août 2022
%{
This script allows the users to input two vectors and then compute their dot product , cross product , sum, and diference
%}
clear
a=input('Enter vector a in the form [ x y z] :\n');
b=input('Enter vector vb in the form [ x y z] :\n');
if isempty(a)
a=[0 0 0];
end
if isempty(b)
b=[0 0 0];
end
ua=a./norm(a);
ub=b./norm(b);
d=dot(a,b);
c=cross(a,b);
sum=a+b;
di=a-b;
fprintf('the unit vector of a is %0.3f \n',ua)
fprintf('the unit vector of b is %0.3f \n',ua)
fprintf('the dot product a.b= %0.3f and the cross product axb= %0.3f \n',d , c)
fprintf('the sum of a+b= %0.3f and the difference of a-b= %0.3f \n',sum , di )

Réponses (1)

Star Strider
Star Strider le 28 Août 2022
Provide numeric field designatoors for each element in the unit vectors (I added the square brackets, remove them if yoou don’t want them) —
a = [1 2 3];
b = [4 5 6];
ua=a./norm(a);
ub=b./norm(b);
d=dot(a,b);
c=cross(a,b);
sum=a+b;
di=a-b;
fprintf('the unit vector of a is [%0.3f %0.3f %0.3f]\n',ua)
the unit vector of a is [0.267 0.535 0.802]
fprintf('the unit vector of b is [%0.3f %0.3f %0.3f]\n',ua)
the unit vector of b is [0.267 0.535 0.802]
fprintf('the dot product a.b= %0.3f and the cross product axb= %0.3f \n',d , c)
the dot product a.b= 32.000 and the cross product axb= -3.000 the dot product a.b= 6.000 and the cross product axb= -3.000
fprintf('the sum of a+b= %0.3f and the difference of a-b= %0.3f \n',sum , di )
the sum of a+b= 5.000 and the difference of a-b= 7.000 the sum of a+b= 9.000 and the difference of a-b= -3.000 the sum of a+b= -3.000 and the difference of a-b= -3.000
.
  3 commentaires
Walter Roberson
Walter Roberson le 28 Août 2022
I recommend compose() for this purpose.
Star Strider
Star Strider le 28 Août 2022
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by