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

 Réponse acceptée

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 commentaires

oh okay i got it now it works well :) thank you.
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?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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!

Translated by