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

1 Ansicht (letzte 30 Tage)
Tracy
Tracy am 1 Okt. 2021
Kommentiert: dpb am 1 Okt. 2021
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

Akzeptierte Antwort

Jan
Jan am 1 Okt. 2021
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

Weitere Antworten (1)

dpb
dpb am 1 Okt. 2021
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 Kommentare
Tracy
Tracy am 1 Okt. 2021
Thank you so much for your help I appreciate it
dpb
dpb am 1 Okt. 2021
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by