Single line of code for vectorization, Need Help!!

Hi there, I need to write just one line of code in order to calculate the corresponding volumes of cones fromed from a vector r of radii and a vector h of heights. Using V =1/3pir^2h for the volume of a cone im supposed to make just ONE line of code in matlab that satisfies this. It says r and h are should be assumed to be row vectors and can be any size.
Here is my code which is probably way off and the grader file i have says it is incorrect. I also have the original question attached.
V = 1/3*pi*r.^2.*h ;

3 件のコメント

Mohammad Sami
Mohammad Sami 2020 年 3 月 31 日
If I understand correctly, the question is asking you to calculate for every combination of r and h given to you.
Add a transpose to h to make it to column matrix, now your formula will create a matrix of volume for all combinations of r and h
V = 1/3*pi*r.^2.*h' ;
Andrei Bobrov
Andrei Bobrov 2020 年 3 月 31 日
V = 1/3*pi*r(:)'.^2.*h(:);
Alex Platt
Alex Platt 2020 年 4 月 2 日
Thank you guys for the help, Mohammad had the correct code for it to work.

サインインしてコメントする。

回答 (1 件)

KSSV
KSSV 2020 年 3 月 31 日

0 投票

r = [0.1 0.2 0.3 0.4 0.5] ;
h = [0.2 0.3 0.4 0.5] ;
[r,h] = meshgrid(r,h) ;
v = 1/3*pi*r.^r.*h ;

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

タグ

質問済み:

2020 年 3 月 31 日

コメント済み:

2020 年 4 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by