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 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