Generate a 10-digit random number [closed] - matlab

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to generate a random number with exactly 10-digits.
The random number cannot begin with zero, i.e. it must be a 10 digit number, not 10 random digits.

You need a random integer, between 1000000000 (the lowest integer with 10 digits) and 9999999999 (the highest number with 10 digits).
Note that 1000000000 = 1e9 and 9999999999 = 1e10 - 1
The random integer generation can easily achieved with randi (see the documentation here), giving it the correct minimum and maximum values...
n = randi([1e9, 1e10-1])

Related

Adding numbers whose sum is greater than 10 in brainfuck [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Is there an efficient algorithm that determine the sum of two numbers, even if these numbers are greater than ten?
the user can only enter the numbers one digit at a time. Assuming only positive numbers can be entered the algorithm to parse it is as follows.
initialize accumulator to 0
for each digit the user enters
multiply the accumulator by 10
add the new digit to the accumulator
You will need to handle when the user enters the enter key to finish entering the number, and the above algorithm only works for numbers up to 255 (assuming 8 bit cells).
From then on you have a cell with a number. Do this again to get a cell with another number, then you can simply add them together normally.
Using http://fatiherikli.github.io/brainfuck-visualizer/ you can see the value of each cell numerically, not having to use all that dumb ASCII conversion stuff. Then, just program in your inputs and the algorithm.
++++++>+++++ 6 plus 5
[<+>-]
Adds one to 6 and subtracts one from 5 each iteration, making 11.
The visualizer will show that the cell is 11.
If you want to preserve the y value, add a "temp" cell
++++++>+++++>[-] #0: 6 #1: 5 #2: 0
<[<+>>+<-]
>[<+>-]

Matlab precision [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 7 years ago.
I have a question about the precision in Matlab. I searched the questions here and they mention that there are precision problems when using very large or very small numbers and other posts looking at the comparison of numbers. My case is a bit different
I do not have that, my numbers have only 4 decimal places and I want them to add up to one.
Example:
Initial data:
aeb =
0.231215677537590 0.470472652172102 0.203009018534716
0.087759104877338 0.007588684370711
Then I rounded it to get 4 decimals:
aeb = round(aeb*10000)/10000
0.231200000000000 0.470500000000000 0.203000000000000
0.087800000000000 0.007600000000000
Then I identify the largest number and replace it by the difference with one
[~, idx]=max(aeb);
aeb(idx)=0;
aeb(idx)= 1 - sum(aeb);
But when I then do:
1 - sum(aeb)
1.110223024625157e-16
Does anyone know how I can make them add to one ? I just want 3-5 decimal places.
Their sum is within machine epsilon of the number one. The short answer is that any difference that small is indistinguishable from zero.
A practical point to be aware of....
In a lot of computing contexts, you don't want to test for equality, you want to test if the difference is within a certain tolerance. Eg.
abs(x - y) < tol

Why UInt64.max / 2 + 1 represented in the memory the same as Int64 value -9 223 372 036 854 775 808 but not as -1? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
UInt62.max / 2 is represented by 0100..0000 in the memory. Add 1 and it will be 0100..0001. So, the first bit for the sign. And we take -1. But CPU thinks that it's -9 223 372 036 854 775 808. Why does it work so complexly?
You can see that it's truth because of the issue in the Swift playground: Why is UInt64 max equal -1 in Swift?
var max = UInt64.max / 2 + 1 // playground shows -1 because it treats it as Int64
Yes, in fact -1 is not represented as 1 with a sign bit, but rather as all bits set to one. This is called a "two's complement" representation, and is used in most of the modern processors.
Read more about it here:
https://en.wikipedia.org/wiki/Two%27s_complement
One of the reasons for that is that this way arithmetic operations that involve both negative and positive numbers are easier. If -1 was represented as 1 with a sign bit, and we attempted to add 1 to it in a naive way, we would get 2 with a sign bit instead of zero. With two's complement representation you can just add the numbers as if they were unsigned, and get the correct result.

Using repmat() in MATLAB [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to generate the trial order of my stimuli for an experiment consisting of 10 experimental blocks:
There should be 100 trials per block.
There are 20 images as stimuli.
Within each block, the 20 stimuli should
be shown 5 times each.
The order of the stimuli should be fully randomized.
(Meaning, NOT 1:20 in random order, then 1:20 in random order, and so on.
All 100 trials should be randomized across each block!)
I have to make a matrix that represents the trial order of my experiment, in which the rows represent the 10 blocks, and the columns represent the stimuli to
be shown in order from column 1 - to column 100.
I figured out that I should use the function repmat(), but I can't solve this.
This will do it, just adjust your values for the number of blocks and block size according to your needs. No repmat used though.
Nblocks = 10;
Nchoices = 20;
Ndisp = 5;
Ntrials = Ndisp*Nchoices;
array = ceil([Nchoices/Ntrials:Nchoices/Ntrials:Nchoices]);
perms = array(cell2mat(cellfun('randperm',mat2cell(Ntrials*ones(Nblocks,1),ones(Nblocks,1),1),'UniformOutput',0)));
It's a good idea to split the longer wrapped command into individual steps if you want to make sense of it in more depth. Look in particular at the documentation for individual functions and particularly ceil and randperm.

In matlab why is the 1st digit in a binary notation being discarded? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When i try to display binary notations which start with a zero in the 1st bit position, matlab discards the zero and displays only the other 7 bits. How do I display the 1st position too?
ex: when i try to display "01101111", matlab displays it as "1101111", but I need the 1st bit position value also. Can some one please help.
In Matlab, to display the bit representation of a number you need to convert it into a string with dec2bin().
So, if you have x = 111, it's binary representation is:
dec2bin(111)
ans =
1101111
which retains only the significant bits. To force an 8-bit representation use:
dec2bin(111,8)
ans =
01101111
Note, how the result will be a string. If you want to retrieve bits in numeric format, then use bitget():
bitget(111,8:-1:1)
ans =
0 1 1 0 1 1 1 1
Basically, if your need is purely visual, use dec2bin2() otherwise for manipulating bits, use the bit-wise operations functions, which accept and return numeric types.