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

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.

Related

Problem with displaying the elements of a cell array [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 9 months ago.
Improve this question
I have tried to create vector matrix array, to add integer elements after their arrival.
a={[1 1 1 1 1]; [3 3 3 3 3 3 3 3 3]; [4 4]; [5]};
print(a);
That code gives me this error:
You should take a look at the documentation for print. It is used to:
Print figure or save to specific file format
What you want is either disp which is used to
Display value of variable
Or fprintf which is used to:
Write data to text file (which can be the console)
Or even simpler: Just write
a % Note the absence of ';'
Upon encountering an operation without semicolon, MATLAB aromatically displays the result in the console. So this is enough to print you variable.

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
<[<+>>+<-]
>[<+>-]

Compare variable is greater/less in Matlab [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 7 years ago.
Improve this question
how to determine is a variable is greater or equal to a value in MATLAb , I tried :
if (myVar >= '123.1')
do mething here
else
do other thing
end
but it seems doing other thing each time !!
I see warning on '123.1' saying that is seldom , is there a function to compare ?
Comparing characters logically performs an implicit type conversion prior to comparison (I'm assuming using double).
So when you type '123' >= '234' you are actually performing:
[double('1') >= double('2'), ... % 49 >= 50
double('2') >= double('3'), ... % 50 >= 51
double('3') >= double('4') ... % 51 >= 52
];
This will error if your strings are different lengths. Convert your data for comparison with something like str2double or str2num prior to entering your logic statement.
For example:
strvar = '123.2';
if str2double(strvar) >= 123.1
disp('True!')
else
disp('False!')
end
With the appropriate floating point approximation caveat

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.

Replacing the NaN and Inf Values [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 8 years ago.
Improve this question
I am doing feature extraction for a data set, while calculating the slope feature, its value is sometimes float number or NaN or -ve it +ve Inf.
example { 1.525474574, NaN , -1.056217 ,-Inf , Inf }
the NaN and Inf values causes a problem while training the system, so i am planing to replace their values with numeric values, but i don't with what values should i replace them ?
if i found Inf, should i replace it with large number ? and -Inf by very small ? , what about NaN ?
any help !!
As is this is not a matlab question. I am not even sure it is a progamming question, but perhaps this helps:
If you really want to keep incorrect values in your data, you should replace them with the most meaningfull substitute. (Though of course you would preferably prevent these values from coming into existance at all)
Without knowledge of how those values became NaN for example, there is no way to say what the 'best' way is to deal with them.
My thought: If you don't know what you are dealing with, just remove them, don't try to 'fix' them as you may do more harm than good.