1 view (last 30 days)

Hello everyone i was wondering if u could help me multiply these two increasing variables. They just do not multiply correctly

v0 = (10:1:20);

theta = (30:1:40);

v0_x = v0.*cosd(theta);

v0_y = v0.*sind(theta);

Shae Morgan
on 14 Aug 2020

Edited: Shae Morgan
on 14 Aug 2020

Is this more what you were looking for?

v0 = (10:1:20);

theta = (30:1:40);

for i= 1:length(v0)

for j = 1:length(theta)

v0_x(i,j)=v0(i)*cosd(theta(j));

v0_y(i,j)=v0(i)*sind(theta(j));

end

end

v0_x

v0_y

KSSV
on 14 Aug 2020

If you wanted that..simply use:

v0 = (10:1:20);

theta = (30:1:40);

[v0,theta] = meshgrid(v0,theta) ;

v0_x = v0.*cosd(theta);

v0_y = v0.*sind(theta);

Shae Morgan
on 14 Aug 2020

meshgrid() is another great solution here

x=[1 2 3]; %x array

y=[3 2 1]; %y array

[X,Y]=meshgrid(x,y)

%X is a length(y) long set rows of x

%Y is a length(x) long set of columns of y

using these matrices with the point multiplication will give the same outcome. The for-loops feel more intuitive and are visually understandable as you read through the code, but meshgrid requires fewer lines and a little more understanding of what it's doing.

In the end it's preference. I prefer code readability (or familiarizing yourself with new functions so you can improve readability!)

Bruno Luong
on 14 Aug 2020

Edited: Bruno Luong
on 14 Aug 2020

v0 = (10:1:20);

theta = (30:1:40);

v0_x = v0.*cosd(theta.'); % horizontal .* vertical vectors

v0_y = v0.*sind(theta.');

Opportunities for recent engineering grads.

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

Start Hunting!
## 6 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/579570-how-do-i-multiply-2-increasing-variables#comment_972570

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/579570-how-do-i-multiply-2-increasing-variables#comment_972570

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/579570-how-do-i-multiply-2-increasing-variables#comment_972573

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/579570-how-do-i-multiply-2-increasing-variables#comment_972573

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/579570-how-do-i-multiply-2-increasing-variables#comment_972615

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/579570-how-do-i-multiply-2-increasing-variables#comment_972615

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/579570-how-do-i-multiply-2-increasing-variables#comment_972654

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/579570-how-do-i-multiply-2-increasing-variables#comment_972654

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/579570-how-do-i-multiply-2-increasing-variables#comment_972684

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/579570-how-do-i-multiply-2-increasing-variables#comment_972684

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/579570-how-do-i-multiply-2-increasing-variables#comment_972702

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/579570-how-do-i-multiply-2-increasing-variables#comment_972702

Sign in to comment.