Convert decimal to binary in matlab [duplicate] - matlab

This question already has answers here:
Convert Decimal to Binary Vector
(4 answers)
Closed 7 years ago.
If I use a decimal to binary command in Matlab how can I get the result in four digits? e.g. 7 =0111 or 2=0010.
Thanks in advance
Othman

Use dec2bin:
str = dec2bin(7) // 0111
str = dec2bin(7,8) // 00000111, pad the result to 8 bits

Related

With Swift How Can I Convert String To Double one-to-one [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
swift: issue in converting string to double
(2 answers)
Swift double to string
(16 answers)
Closed 22 days ago.
While converting the string to double, I cannot do exactly the same. how can i fix this
StringValue = "0.00001174"...DoubleValue = Double(StringValue)...
print(DoubleValue) ->"0.000011749999999999999"
How can I get same . Thanks

Why 100/1000 is not 0.10 in swift palyground? [duplicate]

This question already has answers here:
Math divison in Swift
(4 answers)
Closed 2 years ago.
I am facing a problem where I am not getting values of decimal places here is the code that I used to run in Swift Playground
print(100/1000)
print(Float(100/1000))
Expected Output :
0.10
0.10
Actual Output:
0
0.0
Because of this:
let variable = 100/1000
print(type(of: variable))
// prints Int
Just do the following:
print(100.0 / 1000.0)
print(Float(100) / Float(1000))
So in order to get a floating point result you need to divide floating point numbers instead of two integers

How can I get unicode number of a character? [duplicate]

This question already has answers here:
How to convert an Int to Hex String in Swift
(6 answers)
Closed 6 years ago.
How can I get the Unicode number of a given string ?
For example, Cyrillic Small Letter Er U+0440 stands for "р"
How can I get "U+0440" or "0440"?
====Technical information====
Unicode number: U+0440
HTML-code: &#1088
var myString = "р"
for scalar in myString.unicodeScalars {
print("\(scalar.value) ") // print: 1008
}
440 in hex is equal to 1088 in decimal:
var myString = "р"
for scalar in myString.unicodeScalars {
print(String(scalar.value, radix: 16))
}

Is there a way to store the exponent of my vector values as a variable in itself? [duplicate]

This question already has answers here:
How to get Exponent of Scientific Notation in Matlab
(2 answers)
Closed 7 years ago.
testVector =
1.0e+10 *
3.5688 3.1110 5.2349
Is it possible to take out the exponent (not sure what it's called) of a vector and store it as a variable? E.g. in this case the variable would have value 1.0e+10
You can find the value of the exponent using log10:
testVector = [3.5688e+10 3.1110e+10 5.2349e+10];
lowExp = min(floor(log10(testVector)));
eVal = 10^lowExp;
Result:
eVal = 1.0000e+10
Then you'll need to divide your original vector by eVal:
newTestV = testVector/eVal
newTestV =
3.5688 3.1110 5.2349

Matlab, how to convert a string of integers into a vector? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
convert string to number array in matlab
Is there a simple way in Matlab to convert a string like this
'123456789'
into a vector like this ?
[1 2 3 4 5 6 7 8 9]
If all you have is contiguous characters from 0 to 9:
v = double(s)-'0';
double(s) converts a string into an array where each element is the ASCII code of the corresponding character. To obtain the numberic values we subtract '0' (which is in fact 48 in ASCII) and since digits have a sequential representation in ASCII code ('1' = 49, '2' = 50, etc.) we end up with intended result.
one way would be using regexp for this. But of course it only works for single digit numbers.
>> str = '123456789';
>> num = regexp(str,'\d')
num =
1 2 3 4 5 6 7 8 9