Help with a basic MATLAB question
Show older comments
Hey, i'm learning how to use MATLAB and I was confused on what was meant by the question. Can someone explain how I can decompose the number using rem please?
?
4 Comments
Rik
on 3 May 2019
The task is to turn the number into a vector of its decimal components. So for 864736 you need to find a way to turn that into a vector [8 6 4 7 3 6]. Your teacher wants you to use the rem function to find those values.
You should read the documentation (type doc rem in the command window).
Since the ID has a relatively small length I would propose a different method: turn it into a char array (see sprintf) and then cast it back to a double.
Jacob
on 3 May 2019
Rik
on 5 May 2019
Please don't delete your question text. It is extremely rude to get free help first and then remove the potential for any future reader to benefit as well.
Rena Berman
on 13 May 2019
(Answers Dev) Restored edit
Answers (1)
Guillaume
on 3 May 2019
2 votes
Given a positive integer, e.g. x = 749623, you're supposed to decompose it into a vector of digits, for my example y = [7, 4, 9, 6, 2, 3].
- What is the remainder of 749623 / 10 ?
- What is the remainder of 74962 / 10 ?
- What is the remainder of 7496 / 10 ?
- etc.
That should be enough for you to understand what you have to do.
16 Comments
Jacob
on 3 May 2019
Steven Lord
on 3 May 2019
As an example, 23 is 2*10 + 3.
How would you decompose 1234 in a similar way?
Jacob
on 3 May 2019
Guillaume
on 3 May 2019
Where does the 10 come from in the question
Your fingers! We've been using the decimal system for a while now...
You do not decompose 1234 into 10*123 + 4. Come on, you've been taught that in primary school or even earlier. Units, Decades, Hundreds, Thousands, etc.
Jacob
on 3 May 2019
Rik
on 3 May 2019
%So for two integers a and b
rem(a,b)
%returns a value c such that
a=m*b+c
%where m is an integer m, and c<b
How do you think this relates to the decomposition here?
Jacob
on 4 May 2019
Rik
on 4 May 2019
It is a step by step process:
- Given 1234, what is the last digit? That has the same answer as finding the remainder for 1234/10.
- Now we have a variable with the value 4, how can we convert 1234 to 123 to get it ready for the next step? Subtract the remainder and apply the division: (1234-4)/10.
- Now we have 123, what is the last digit? That has the same answer as .......
- ....
In the end you have a variable for each digit of your input number (in this case the ID).
Jacob
on 4 May 2019
Walter Roberson
on 4 May 2019
six = rem(YourID, 10);
rest = (ID - six)/10;
five = rem(rest, 10);
rest = (last - five)/10;
four = rem(rest, 10);
rest = (last - four)/10;
three = rem(rest, 10);
rest = (last - three)/10;
two = rem(rest, 10);
one = (last - two)/10;
Jacob
on 4 May 2019
Rik
on 4 May 2019
Read the documentation for anonymous functions.
Jacob
on 4 May 2019
Walter Roberson
on 4 May 2019
f = @(x) one*x.^2 + two*x + three;
fplot(f, [-5 5])
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!