For loop for string elements and data on a column

2 ビュー (過去 30 日間)
Michael Angeles
Michael Angeles 2022 年 1 月 9 日
コメント済み: Michael Angeles 2022 年 1 月 9 日
How can I create a for loop that would create a "Planets" column on the left hand side while the gravity and distance data are also printed out relative to the planet name:
For example:
Planet gravity_data distance_data
xxxx xxxxx xxxx
My code is below: Thanks in advance...
%Free Fall
clear, clc
planet = ["Mercury", "Venus", "Earth", "Moon", "Mars", "Jupiter", "Saturn",...
"Uranus", "Neptune", "Pluto"];
gravity = [3.7, 8.87, 9.8, 1.6, 3.7, 23.12, 8.96, 8.69, 11.0, .58];
time = linspace(0,100,10);
%d = 1/2*(gravity.*time.^2)
[G, T] = meshgrid(gravity, time)
d = 1/2.*(G.*T.^2)
for i = 1:length(planet)
planet(i,:) = [G "_" num2str(i,"%.2f")];
end

採用された回答

Voss
Voss 2022 年 1 月 9 日
%Free Fall
clear, clc
planet = ["Mercury", "Venus", "Earth", "Moon", "Mars", "Jupiter", "Saturn",...
"Uranus", "Neptune", "Pluto"];
gravity = [3.7, 8.87, 9.8, 1.6, 3.7, 23.12, 8.96, 8.69, 11.0, .58];
time = linspace(0,100,10);
%d = 1/2*(gravity.*time.^2)
[G, T] = meshgrid(gravity, time);
d = 1/2.*(G.*T.^2);
for i = 1:numel(planet)
fprintf('%s %.2f\n',planet(i),gravity(i));
end
Mercury 3.70 Venus 8.87 Earth 9.80 Moon 1.60 Mars 3.70 Jupiter 23.12 Saturn 8.96 Uranus 8.69 Neptune 11.00 Pluto 0.58

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by