how to correctly multiply 2 matrix? [duplicate] - matlab

I have this problem that has been bothering me for quite a while.. I want to change the format of the number.. Don`t know how? Ive tried the help files but can't find the answer.. If you can help me please do..

There are three different things you could potentially be referring to when you talk about the "format" of a number: the display format, the variable storage format in memory (i.e. the data type/class), and the storage format in a data file. I'll address each...
Display format: As already mentioned by Amro, the display format can be adjusted with the FORMAT command. However, this only affects how the numbers are displayed, not how they are stored in memory.
Variable types/classes: There are a number of numeric classes, both floating point and signed/unsigned integer types, that variables can use. By default, MATLAB variables are stored as double-precision floating point numbers. To convert to other types, you can use the variable type as a function to recast a value. For example, a = uint8(0); converts 0 to an 8-bit unsigned integer type and stores it in a, while b = single(pi); converts the value pi into single-precision and stores it in b.
File storage format: When reading/writing numeric values to files, the type of file affects how it will be stored. A binary file (written to and read from using FWRITE and FREAD) stores the complete binary representation of a number, and can thus represent it as exactly the same type as it is stored in memory as a variable (double-precision, single-precision, 8-bit integer, etc.).
Alternatively, a text file (written to and read from using FPRINTF and FSCANF, among other functions) will store a value in a string format that you must specify when outputting the values to the file. You can specify the digits of precision, as well as the format (such as exponential notation or hexadecimal). The documentation for FPRINTF specifies these output formats.

Use the format command
Example:
format long; pi
3.141592653589793
format short e; pi
3.1416e+000
format short g; pi
3.1416

sprintf('%.2f', 1.4795e4);
(in particular: if you want it displayed/saved/printed a certain way, be explicit about it!)

Related

Getting max possible value of integer data type in Octave

I use GNU Octave, version 6.4.0. Is there any command for getting size of usual data classes, at least for integral data types? Really I want to get max possible value for data type of a matrix which is related to an image. For example for an uint8 image such I it must return 256 for argument class(I). I am looking for a builtin command and do not want to write a switch myself.
The intmax() function does what you want for integer types. Either intmax("uint8") or intmax(a) where a is of type uint8 (or any other integer type).
https://octave.sourceforge.io/octave/function/intmax.html
A similar function realmax() exists for floating point types.

Why can't MATLAB save uint8 type matrix?

Here is the code:
x = rand(5)*100;
save('pqfile.txt','x','-ascii','-tabs')
The above works, but:
x = rand(5)*100;
x = uint8(x);
save('pqfile.txt','x','-ascii','-tabs')
says:
Warning: Attempt to write an unsupported data type to an ASCII file.
Variable 'x' not written to file.
Does anyone know why this happens? How come I can't save the data when it is uint8. I have to read data into a VHDL testbench so was experimenting. I guess the only option is to save my 8 bit unsigned integer values in 2d array using printf then read into the test bench.
ASCII option
The save method is somewhat restrictive in what it can support, and then it uses floating point notation to represent your numbers which bloats your file when dealing with a limited range of numbers like you are (i.e. uint8, 0 to 255).
Check out dlmwrite as an alternative (documentation here).
It takes the filename to write/save to, the variable to store, and some additional parameters, like the delimiter you want to separate your values with.
For your example, it looks like this
x = rand(5)*100;
x = uint8(x);
dlmwrite('pqfile.txt',x,'\t');
Binary option
If you are looking to stored your uint8 data as single bytes then you probably want go with a custom binary file instead instead of ASCII. (Yes, you can convert uint8 to single ASCII characters but you run into issues with these values being interpreted with your delimiters; newlines or tabs.)
fid=fopen('pqfile.dat','wb');
if(fid>2)
fwrite(fid,size(x),'*uint8'); % Note: change data type here you are dealing with more than 255 rows or columns
fwrite(fid,x','*uint8'); % Transpose x (with x') so it is stored in row order.
fclose(fid);
else
fprintf(1,'Could not open the file for writing.\n');
end
I'm not sure what type of parser you are using for your VHDL, but this will pack your data into a file with a short header of the expected dimensions followed by one long row of your serialized data.
To read it back in with MATLAB, you can do this:
fid = fopen('pqfile.dat','rb');
szX = fread(fid,2,'uint8');
x = fread(fid,szX,'*uint8')'; % transpose back if you are dealing with matlab.
fclose(fid);
The transpose operations are necessary for MATLAB because it reads data column-wise, whereas most other languages (in my experience) read row-wise.

Bitget working with large inputs?

The default type is uint64 but the below apparently requires larger support where you can see that the number 536870915 (100000000000000000000000000011 in binary, 30 bits' length) is not supported by the above bitget command. So
How to get bitget command working with large inputs like the below?
Input
hhhh=sparse([],[],[],2^40+1,1);
hhhh(536870915)=1;
bitget(str2num(dec2bin(find(hhhh)-1)),2,'uint64')
Output
Error using bitget
Double inputs must have integer values in the range of ASSUMEDTYPE.
You pass the output of dec2bin(find(hhhh)-1) to str2num. This directly converts the string of ones and zeros into a double: 9.999999999999999e+28. I'm guessing that's not what you want.
If you're just trying to get the second bit of 536870915, why not use:
bitget(find(hhhh)-1,2,'uint64')
On the other hand, I think that you could also use this (probably slower, but maybe it'll work with the rest of your code if you're already converting to string representation):
b = dec2bin(find(hhhh)-1);
str2double(b(end-1))
It seems like you're trying to combine two approaches.

Convert string to high precision number in matlab

I have a file with data values of the order 10^(-6).When I try to read it in matlab, it just give me accuracy of 10^(-4).I used like below,
[y]=textread('report.txt','%f')
I tried to change %f to %0.6f, but still it does not work.
Then I try to read file as %s and use str2double, again same result.
0.004586 is just 0.0045
Please help me
Use format to change the precision.
The format function affects only how numbers display in the Command Window, not how MATLAB computes or saves them.
View current format: get(0,'format')
Set current format in present session to long using: format long
Set current format to long for successive session using : set(0,'Format',long)
long format offers 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.
Type help format for more details.
Update the format of your number like this:
matlab>> format long

MATLAB num2str format

I'm storing variable values in MATLAB and putting one of the variable values as part of the file name . e.g. "Error=1e-003.mat", however different version of MATLAB gives me different format when I'm converting numbers to string using num2str command. The number 1e-3, MATLAB2011 gives me 1e-003 and MATLAB2012 gives me 1e-03.
This runs into trouble when I try to load a batch of files with a mix of those two format. Does anyone know a way to add a zero or delete a zero for the exponent so the names are consistent? Or any other ways to solve this problem?
Here's a fairly robust way to do it:
A = num2str(YOUR_NUMBER, '%0.0e');
if A(end-2)=='-' || A(end-2)=='+'
A = [A(1:end-2) '0' A(end-1:end)]; end
In words: convert the number, and check if the second-to-last character is either a '+' or a '-'. If this is so, add a '0'.
Specify a "Format string as the second argument like this:
>> disp(num2str(2920230,'%0.10e'))
2.9202300000e+006
here %0.10e means display at least 0 digits before decimal and exactly 10 digits after it in exponent form.