我使用Matlab从Excel文件中读取。我应该读一个名为Change Due的整数列。在我读完专栏后,我应该在Quarters,Dimes..etc
中计算变化我可以阅读专栏没问题,我理解如何计算变化的数学。 然而使用条件来检查更改量很难......
问题:
我知道对于乘法/矢量分割,您可以添加一个句点,使其成为标量。* 或 ./
但是如何解析具有条件的向量?
if(Change.<25&&Change.>=10)
我收到此错误:
Operands to the || and && operators must be convertible to logical scalar values.
如果我只是简单地将句点留出来,我就不会收到错误消息,但它只会通过第一行进行计算。
代码:
% Filename: Program_04_1
% Author: Stewart Moon
% Assisted by: No one
% Program Description:
% The purpose of this program is to demonstrate how to read from an Excel
% file and to then calculate the amount of coins it will take per row
clc % clc clears the contents of the command window
clear % clear, clears all defined variables form the Matlab workspace
close all % closes all figure windows
% Declare Variables
Quarters=0;
Dimes=0;
Nickels=0;
Pennies=0;
TotalCoins=0;
% Output of the title and author to the command window
fprintf('Output for Program_04_1 written by Stewart Moon.\n')
fprintf('\nOriginal Data read from Program_04_1_Data.xlsx\n')
fprintf('\nMinimum Number of Coins Needed to Make Change\n')
Change=xlsread('Tutorial_04_1_Data.xlsx','Coins','B4:B43'); % Reading the column in Excel and storing it the variable Change
% Output Header Format
fprintf('\nChangeDue(cents) Quarters Dimes Nickels Pennies Total Coins\n\n')
table=[Change];
disp([table])
命令窗口输出:
Output for Program_04_1 written by Stewart Moon.
Original Data read from Program_04_1_Data.xlsx
Minimum Number of Coins Needed to Make Change
ChangeDue(cents) Quarters Dimes Nickels Pennies Total Coins
1
4
5
6
10
14
15
16
20
24
25
29
30
31
35
37
40
42
45
49
50
54
55
56
60
64
65
68
70
73
75
77
80
81
85
88
90
91
95
99
计算更改伪代码:
while(Change>0)
if (Change>=25)
Change=Change-25;
TotalCoins=TotalCoins+1;
Quarters=Quarters+1;
end
if (Change<25&&Change>=10)
Change=Change-10;
TotalCoins=TotalCoins+1;
Dimes=Dimes+1;
end
if (Change<10&&Change>=5)
Change=Change-5;
TotalCoins=TotalCoins+1;
Nickels=Nickels+1;
end
if (Change<5&&Change>=1)
Change=Change-1;
TotalCoins=TotalCoins+1;
Pennies=Pennies+1;
end
end
答案 0 :(得分:2)
根据我的理解,你想要达到如下目的:
for i = 1 : length(Change)
% your conditions using indices for the array
% example:
% if(Change(i) > 25)
% instructions
% end
end