How to write a binary matrix with fixed point elements in matlab? - matlab

I want to write a matrix 25 by 8 with each column represent a bit value and also want to make it a fixed point binary representation i.e. i want to write binary in decimal as
0000.1000 = .5 so in this expression the binary fixed point is at mid
So like wise a matrix
s=[0000.1000;
0000.0100;
........
.....
0000.1100]
I want to keep this format for the whole matrix or something like that and also whos command will show me the class as binary. Thanks

Related

How can i write a number values in powers of 10? [duplicate]

This question already has answers here:
What is the Small "e" in Scientific Notation / Double in Matlab
(2 answers)
Closed 7 years ago.
How can I write a number/Integer value to power of 10, e.g. 1000 as 10^3? I am writing code whose output is a string of very large numbers. My output in longEng format is:
4.40710646596169e+018
16.9749211806197e+186
142.220634811050e+078
508.723835280617e+204
1.15401317731033e-177
129.994388899690e+168
14.3008811642810e+153
1.25899227268954e+165
24.1450064703939e+150
627.108997290435e+144
2.03728822649372e+177
339.903986115177e-066
150.360900017430e+183
5.39003779219462e+135
183.893417489826e+198
648.544709490386e+045
19.7574461055182e+198
3.91455750674308e+102
6.41548629454028e-114
70.4943280639616e+096
19.7574461055182e+198
3.11450571506133e-009
249.857950606210e+093
4.64921904682151e+180
750.343029004712e+147
I want these results to be in a format of power of 10, so that I can easily do arithmetic operations for my next function.
you can write format shortE and see you output like this:
4.4071e+18
1.6975e+187
1.4222e+80
5.0872e+206
If you only want to print the data in scientific format, the Matlab itself can do this for you.
If you can to obtain the scientific notation form as
a * 10^b,
i.e., obtain the coefficient a and the exponent b, you can first obtain the b as:
b = floor(log10(abs(x)));
then the a as:
a = x * 10^(-b);
from my understanding you wish to take your number e.g. 4.40710646596169e+018 and split it up into:
4.40710646596169 and 018 once you have them separated you you can perform operations as you wish.
You can even join them back to look like: 4.40710646596169^018 if you so desire (although to look like that they would be strings and therefore mathematical operations on the number would be NAN).
Since e represents to the power 10 and is present in all numbers you listed this is a simple process with many solutions, here is one.
% format long is very important otherwise it will appear to you that you have
%lost precision. MATLAB hides precision from view to save screen space and to
%produce less confusing results to the viewer. (the precision is still there but
%with format long you will be able to see it.
format long
x = 4.40710646596169e+018;
%convert your number into a string, this will allow you to split the number based
%on the always present e+ 'delimiter' (not really a delimiter but looks like one')
s = num2str(x);
%use strsplit to perform the split in the required place. it will output a 1x2
%cell
D = strsplit(s, {'e+'});
%extract each cell to a separate variable. in fact D{1} can be directly used for
%the input of the next function.
D11 = D{1};
D22 = D{2};
%convert the separated strings back into numbers with double precision (keep
%maintin value accuracy)
D1 = str2double(D11)
D2 = str2double(D22)
in order to do this operation on an entire column vector it is simply a matter of using a for loop to iterate through all the numbers you have.

Matlab string to double (str2double)

I am trying to build a finantial application that handle economical data using Matlab. The file I want to load is in a csv file and are double numbers in this format '1222.3'. So far, I am just working with one dimension and I am able to load the data into a vector.
The problem is that the data is loaded into the vector in String format. To change all the vector into double format I use str2double(vector), but the numbers into the vector end like this:
1222.3 -> 1.222
153.4 -> 0.1534
I have tried to multiply the vector per 100 (vector.*100), but did not work.
Any idea?
If your vector components are sufficiently large enough, MATLAB will print the numbers in exponential format.
>> a = 1234.56
a =
1.2346e+03
The numbers are also shown in scientific notation in the workspace browser:
You can print the numbers in decimal form using e.g. fprintf:
>> fprintf('%5.3f\n',a)
1234.560
>>
As a side note, 1.222 * 100 ≠ 1222 ...
Matlab automatically pulls some common factor out numerical vectors, which has confused me many times myself. The line that gives the common factor is easy to miss, especially for large vectors, because it is displayed at the top.
If I define a vector with the two number you gave, Matlab displays it to me in the following way:
It pulled out a factor of 1000, as indicated by the line 1.0e+03 *.

Converting data into fixed point binary in Matlab?

I am not a regular Matlab user, so I apologize if this question is naïve. I am working on a hardware project and would like to convert some data to fixed point binary using the fixed point toolbox.
All my data are float in nature and in the range of -1 to +1. I was trying to convert them into fixed point in Matlab, to no avail.
I have been getting different types of errors, from "Cell contents assignment to non cell array objects" to just wrong binary values. Below is my code.
for i=1:count
temp=datax(i); % datax is a array of decimal values between -1 and 1
fixeda{i}=bin(sfi(temp,16,15));
% Since all values are in the same range I set the word length to be 16 and fractional part to be 15
end
I'm not a matlab user, but shouldn't the 1.15 representation of a float between -1 and 1 just be int(f*32768)
Note that you can only represent numbers less than 1.0 in this representation. 1.0 exactly causes an overflow.
Looking at the help pages it looks like fixeda = bin(sfi(datax,16,15)) should be all you need.

get vector which's mean is zero from arbitrary vector

as i know to get zero mean vector from given vector,we should substract mean of given vector from each memeber of this vector.for example let us see following example
r=rand(1,6)
we get
0.8687 0.0844 0.3998 0.2599 0.8001 0.4314
let us create another vector s by following operation
s=r-mean(r(:));
after this we get
0.3947 -0.3896 -0.0743 -0.2142 0.3260 -0.0426
if we calculate mean of s by following formula
mean(s)
we get
ans =
-5.5511e-017
actually as i have checked this number is very small
-5.5511*exp(-017)
ans =
-2.2981e-007
so we should think that our vector has mean zero?so it means that that small deviation from 0 is because of round off error?for exmaple when we are creating white noise or such kind off random uncorrelated sequence of data,actually it is already supposed that even for such small data far from 0,it has zero mean and it is supposed in this case that for example for this case
-5.5511e-017 =0 ?
approximately of course
e-017 means 10 to the power of -17 (10^-17) but still the number is very small and hypothetically it is 0. And if you type
format long;
you will see the real precision used by Matlab
Actually you can refer to the eps command. Although matlab uses double that can encode numbers down to 2.2251e-308 the precission is determined size of the number.
Use it in the format eps(number) - it tell you the how large is the influence of the least significant bit.
on my machine eg. eps(0.3) returns 5.5511e-17 - exactly the number you report.

How to store exponential values (1.3e12) and normal values(1.3) in the same matrix in matlab?

I have a file consisting of values ranging from 0.1 to 1.3e12. I have been trying it to store in the same array but its not working. Can anybody help?
The numbers 0.1 and 1.3e12 are both stored in floating point data type. The double type is the default for storing either of these in Matlab. So the answer is Yes you can store them in the same matrix.
What you are actually referring to is the way the numbers are formatted for viewing. Please have a look at the documentation for format