Loop over a list of variables indexed by numbers

3 次查看(过去 30 天)
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops 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