matrix manipulation and transposing
Afficher commentaires plus anciens
so, I am writing this code to Create a table showing the maximum height for the following values of v and q: v = 10, 12, 14, 16, 18, 20 m/s q= 50, 60, 70, 80 The rows in the table should correspond to the speed values, and the column should correspond to the angles. where g=2.8. The formula for the question is v^2*sin(thetha)^2/2*g.
my question is why did i need to transpose v in my code so that it would work?
v=[2:2:20]
g=2.8
thetha=[50:10:80]
h=(v'.^2).*(sind(thetha).^2)./(2*g)
table=[0 thetha; v' h]
Réponses (1)
Because when v is a column vector and theta is a row vector, you get implicit expansion,
8 commentaires
armani canady
le 15 Sep 2020
armani canady
le 18 Sep 2020
If you do .* or ./ operations on vectors of the same shape, e.g., both row vectors, then the operations will be done element-wise,
>> a=[1,2,3]; b=[10,20,30];
>> c=a.*b %This means c(i)=a(i)*b(i)
c =
10 40 90
Conversely, if a is a column vector and b is a row vector, you would be implementing c(i,j)=a(i)*b(j)
>> c = a.' .* b %This gives c(i,j)=a(i)*b(j)
c =
10 20 30
20 40 60
30 60 90
This happens because of a Matlab mechanism called implicit expansion.
armani canady
le 19 Sep 2020
armani canady
le 19 Sep 2020
No, the reason you would do operations between a column vector and a row vector is that you want to take advantage of one of the many uses of implict expansion, illustrated at the link I gave you. The way you choose which vector will be a column and which will be a row just depends on what shape you want the resulting matrix to have and how its contents should be organized. It is not a matter of which vector is longest, as the following examples show:
>> a=[1 2 3]; b=[4 5 6 7];
>> c=a.'+b
c =
5 6 7 8
6 7 8 9
7 8 9 10
>> c=a+b.'
c =
5 6 7
6 7 8
7 8 9
8 9 10
>> c=c+a
c =
6 8 10
7 9 11
8 10 12
9 11 13
>> c=c+b.'
c =
10 12 14
12 14 16
14 16 18
16 18 20
armani canady
le 19 Sep 2020
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!