Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB Use Matlab to write a script and post the script and the result. Write a

ID: 3814518 • Letter: M

Question

MATLAB

Use Matlab to write a script and post the script and the result.

Write a user-defined function that arranges the digits of a given positive) number in a row vector in the same order as they appear in the number; the function should also arrange the digits in the decimal part in a different vector. For example, if the number is 2645.12, the vectors should be [2 6 4 5] and [1 2]. The whole number can be from 0 to 10 digits long and the decimal part 0 to 6. Check the validity of the function using a few numbers of your choice.

Explanation / Answer

We need to write a user defined function which separates the integer and fractional part of a given integer and display it as row vectors.

function [ BeforeDot,AfterDot ] = splitNum( A,~ )

splittedNumber = regexp(num2str(A),'.','split')

[BeforeDot, AfterDot] = splittedNumber{:}

end

The inbuilt matlab function num2str converts our array ,A into a string of numbers or integers and the regexp function returns the starting index of substrings which have been split up using our delimiters specified by split pattern.

The splittednumber is displayed in two different variables , BeforeDot and AfterDot.

We have to display each digit of these in individual vectors as the final result of our user defined function.

Command line argumens

>>splitNum(4578.967)

splittedNumber =

1×2 cell array

    '4578' '967'

ANother example,

>> splitNum(18484848.5858840)
splittedNumber =
1×2 cell array
'18484848' '5859'
BeforeDot =
'18484848'
AfterDot =
'5859'
ans =
'18484848'
>>
>> splitNum(18484848.5858840)

Another example,

>> splitNum(629864840008.689312450008)
splittedNumber =
1×2 cell array
'629864840008' '6893'
BeforeDot =
'629864840008'
AfterDot =
'6893'
ans =
'629864840008'

BeforeDot =

'4578'

AfterDot =

'967'

Say,

BeforeDot=4578

AfterDot=967
y1=num2str(BeforeDot)
y2=num2str(AfterDot)
y3= sscanf(s(1:end), '%d')
y4= sscanf(s(1:end), '%d')

[A ]=strrread(y3,'%d ')

[B]=strrread(y4,'%d ')

A=

4

5

7

8

B=

9

6

7