Loop over a list of variables indexed by numbers

2 ビュー (過去 30 日間)
alpedhuez
alpedhuez 2021 年 8 月 14 日
コメント済み: alpedhuez 2021 年 8 月 14 日
Suppose I have a table
Day1Vistors Day1Sales Day2Visitors Day2Sales
NY 10 100 15 45
SF 5 20 10 50
Then I want to calculate Day1SalesPerVisitor, Day2SalesPerVisitor
I would lke to write a loop over variable names like
for i=1:2
DayiSalesPerVisitor=DayiSales./DayiVisitor;
end
How can one operationalize it?

採用された回答

Matt J
Matt J 2021 年 8 月 14 日
編集済み: Matt J 2021 年 8 月 14 日
Where your table is T,
for i=1:2
DaySalesPerVisitor{i}=T{:,2*i}./T{:,2*i-1};
end
Alternatively,
A=table2array(T);
DaySalesPerVisitor=A(:,2:2:end)./A(:,1:2:end);
  1 件のコメント
alpedhuez
alpedhuez 2021 年 8 月 14 日
Wanted more general method to run loop over variable names but this would surely work.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by