How do i add a for loop if command to a pre existing table

1 回表示 (過去 30 日間)
Tracy
Tracy 2021 年 10 月 1 日
コメント済み: dpb 2021 年 10 月 1 日
Hi im very new to Matlab with student version, no add on's I need to include a loop if command into a table, the following is my code
shop1=[4,5,3,6,0,5,5,6,4,5];
shop2=[5,3,1,1,3,5,3,6,3,3];
day=1:10;
profit1=3*shop1;
profit2=4*shop2;
disp(' day shop1 profit1')
disp(' ')
disp([day', shop1', profit1'])
disp(' ')
disp(' day shop2 profit2')
disp(' ')
disp([day', shop2', profit2'])
disp(' profit1 profit2')
disp([profit1', profit2'])
for d=1:10; % I need to insert these results as a third column of the profit1 profit2 table
if profit1(d)>profit2(d)
disp('Shop1 is leading')
elseif profit2(d)>profit1(d)
disp('Shop2 is leading')
elseif profit1(d)==profit2(d)
disp('It is a tie')
end
end
sales_equal=find(shop1==shop2);
disp('Days which sales were the same')
disp(sales_equal)
disp(' ')
disp('Number of days sales were the same')
disp(length(sales_equal))
any help is greatly appreciated thank you

採用された回答

Jan
Jan 2021 年 10 月 1 日
shop1 = [4,5,3,6,0,5,5,6,4,5];
shop2 = [5,3,1,1,3,5,3,6,3,3];
profit1 = 3 * shop1;
profit2 = 4 * shop2;
pool = {'Shop1 is leading', 'It is a tie', 'Shop2 is leading'};
compare = pool(sign(profit2 - profit1) + 2);
allData = cat(1, num2cell(profit1), num2cell(profit2), compare);
disp (' profit1 profit2 compare');
profit1 profit2 compare
fprintf(' %-13d%-13d%s\n', allData{:});
12 20 Shop2 is leading 15 12 Shop1 is leading 9 4 Shop1 is leading 18 4 Shop1 is leading 0 12 Shop2 is leading 15 20 Shop2 is leading 15 12 Shop1 is leading 18 24 Shop2 is leading 12 12 It is a tie 15 12 Shop1 is leading
  1 件のコメント
Tracy
Tracy 2021 年 10 月 1 日
Thank you so much for your help I appreciate it

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

その他の回答 (1 件)

dpb
dpb 2021 年 10 月 1 日
Begin learning to use MATLAB vector operations from the git-go -- naming variables sequentially with a numeric suffix is almost always a sure sign to use arrays instead. When don't then have to write duplicate code or use arcane, slow and very prone to error routes to avoid.
Use the builtin table for the display feature as well in the command window--something otoo...
shop=[4,5,3,6,0,5,5,6,4,5;
5,3,1,1,3,5,3,6,3,3].'; % Use an array oriented by column
profit=[3 4].*shop; % compute profits with automagic array expansion (note "dot" operator here)
tShopProfits=array2table([shop profit],'VariableNames',{'Shop 1','Shop 2','Profits 1','Profits 2'}); % build base table
tShopProfits=tShopProfits(:,[1 3 2 4]); % rearrange column order for convenient viewing
tShopProfits.Leader=string(compose('Shop %d',(tShopProfits.("Profits 1")<tShopProfits.("Profits 2"))+1)); % populate the winner column
isTie=tShopProfits.("Profits 1")==tShopProfits.("Profits 2"); % and fix up for ties
tShopProfits.Leader(isTie)=repmat("Tied",sum(isTie),1); % write the tied value where needed
The above yields--
>> disp(tShopProfits)
Shop 1 Profits 1 Shop 2 Profits 2 Leader
______ _________ ______ _________ ________
4.00 12.00 5.00 20.00 "Shop 2"
5.00 15.00 3.00 12.00 "Shop 1"
3.00 9.00 1.00 4.00 "Shop 1"
6.00 18.00 1.00 4.00 "Shop 1"
0.00 0.00 3.00 12.00 "Shop 2"
5.00 15.00 5.00 20.00 "Shop 2"
5.00 15.00 3.00 12.00 "Shop 1"
6.00 18.00 6.00 24.00 "Shop 2"
4.00 12.00 3.00 12.00 "Tied"
5.00 15.00 3.00 12.00 "Shop 1"
>>
  3 件のコメント
Tracy
Tracy 2021 年 10 月 1 日
Thank you so much for your help I appreciate it
dpb
dpb 2021 年 10 月 1 日
Glad to...if it does solve your problem, go ahead and Accept the answer if for no other reason than to indicate to others there is a solution.

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

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by