how to fill a 2D array using for loops with given ranges

so i am given a problem in which i have a range for speed and density and i need to create an mxn matrix that contains the values of the power, by varying the air speed in the range of 15 m/s < V < 35 m/s and the air density range of 1.11 kg/m3 < density < 1.29 kg/m3. the power is given by a formula between speed and density. but i don't know how to fill this particular array whose dimensions are 19 rows (for the density) by 21 columns (for the speed). any suggestions ? Below is my attempt to try and fill the array.
height = input('enter the height of the car in meters: ');
width = input('enter the width of the front of the car in meters: ');
frontal_area = height * width;
v = [15:35];
roh = [1.11:0.01:1.29];
k = 0;
l = 0;
p = [];
for k = 1:length(v)
for l = 1:length(roh)
p = 0.2 * roh(l) * v(k).^3 * frontal_area
end
end

 採用された回答

Image Analyst
Image Analyst 2016 年 5 月 29 日

1 投票

You need to index p:
for col = 1:length(v)
for row = 1:length(roh)
p(row, col) = 0.2 * roh(row) * v(col).^3 * frontal_area
end
end

3 件のコメント

Elio Faddoul
Elio Faddoul 2016 年 5 月 29 日
oh okay i got it now it works well :) thank you.
Sariha Azad
Sariha Azad 2019 年 5 月 31 日
What if I want to create a 2 D matrix using the loop conditions, as, I don't know the row number, the column number is 2, each row will look like [i j]. How will I get that?
Alex Wang
Alex Wang 2020 年 9 月 17 日
shabi

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by