Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 9 years ago

# 請教各位Matlab高手

ISBN numbers (since 2007) for book identification are 13-digit integers which have a

very special property to ensure that they are valid. This is called the “1-3-sum”, and

is calculated by multiplying the first digit by 1, the 2nd digit by 3, the 3rd by 1, the 4th

by 3, etc, all the way to the last digit which is multiplied by 1. These values are then

added together to get the final 1-3-sum. If the ISBN number is valid, this sum will be

a multiple of 10. Write a MATLAB function which takes as input a single number

(the ISBN number) and then returns whether the number is a valid ISBN number or

not. To do this, you should use a local function (within your function) called

one_three_sum, which calculates the 1-3-sum of the number. Test the function by

using the ISBN of any books you may have (note that the BARCODE is usually the

correct 13-digit ISBN for older books)

--------------------------------------------------------------------------------------------------------------------

The factorial of a number is defined as n! = 1 for n = 0, otherwise n! = n * (n-1)!. For

example, 5! = 5x4x3x2x1 = 120. Write a MATLAB function which calculates the

factorial of a number and returns the result. Note that your function should first check

that the number is both an integer and non-negative, as negative factorials are not

defined. You can use the isnumeric() and isfloat() functions to check if a number is

an integer or not. Test your function to make sure it outputs the correct results (don’t

go too high, the numbers get very large!).

Update:

Update 2:

Rating

第一題:

要給一個barcode

我隨便拿兩個來試試

>> barcode1 = [9 7 8 9 8 6 1 6 6 1 1 7 9];

>> barcode2 = [4 7 1 0 4 9 8 6 0 4 0 4 3];

>> sumT1 = one_three_sum(barcode1)

sumT1 =

150

>> sumT2 = one_three_sum(barcode2)

sumT2 =

110

------------------------------------------------------------------程式碼如下

function sumT = one_three_sum(barcode)

m = length(barcode);

a = zeros(1,m);

for k = 1:m

if mod(k,2) == 0

a(k) = 3;

else

a(k) = 1;

end

end

sumT = sum(a.*barcode);

第二題

要給一個n值, 我拿n =1, n = 5試試

>> n = 1;

>> number = factorial(n)

number =

1

>> n = 5;

>> number = factorial(n)

number =

120

------------------------------------------------------------------程式碼如下

function number = factorial(n)

if ~ isnumeric(n)

disp('n must be a numerical number')

end

if sign(n) == -1

disp('n must be positive')

end

A = [];

for k = 1:n

A = [A,k];

end

number = prod(A);