Matlab matrix Multiplication error - matlab

When I am using the following line of code in Matlab for the multiplication of two matrices
multiplied = singleMat * singleMatT;
Then it gives me this error..
??? Error using ==> mtimes
Integer data types are not fully supported for this operation.
At least one operand must be a scalar.
Please help me out for the multiplication of two matrices in matlab..

I guess Matlab doesn't support matrix multiplication of integer matrixes. Try:
multiplied = double(singleMat) * double(singleMatT);
or
multiplied = single(singleMat) * single(singleMatT);
if single precision is enough.

Related

Error using multiplication matlab

I am trying to plot this function in matlab but I get the error: Error using *
Inner matrix dimensions must agree
Why is this happening?
My code:
H_s=2;
f_zero=2;
f=0:0.001:0.01;
S_f=(5*(H_s).^2)/(16*f_zero)*(f/f_zero).^(-5)*exp(-(5/4)*f)
plot(f,S_f)
The first terms ((5*(H_s).^2)/(16*f_zero)*(f/f_zero).^(-5)) evaluate to a 1x11 matrix, as does the last term (exp(-(5/4)*f)). The matrix multiplication operator * indeed requires the inner dimensions to match.
But you were probably trying to do element-wise multiplication .*:
S_f=(5*(H_s).^2)/(16*f_zero)*(f/f_zero).^(-5).*exp(-(5/4)*f)

MTIMES is not fully supported for integer classes. At least one input must be scalar

I'm trying to implement a 1 dimensional DFT without using Matlab built-in functions such as fft(). This is my code
function [Xk] = dft1(xn)
N=length(xn);
n = 0:1:N-1; % row vector for n
k = 0:1:N-1; % row vecor for k
WN = exp(-1j*2*pi/N); % Twiddle factor (w)
nk = n'*k; % creates a N by N matrix of nk values
WNnk = WN .^ nk; % DFT matrix
Xk = (WNnk*xn );
when i run the code after using the following commands:
I = imread('sample.jpg')
R = dft1(I)
I get this particular error:
Error using *
MTIMES is not fully supported for
integer classes. At least one input
must be scalar.
To compute elementwise TIMES, use
TIMES (.*) instead.
Can someone please help me to figure out how to solve this problem
Note: I am still in the very beginning level of learning Matlab
thank you very much
You just need to cast the data to double, then run your code again. Basically what the error is saying is that you are trying to mix classes of data together when applying a matrix multiplication between two variable. Specifically, the numerical vectors and matrices you define in dft1 are all of a double type, yet your image is probably of type uint8 when you read this in through imread. This is why you're getting that integer error because uint8 is an integer class and you are trying to perform matrix multiplication with this data type with those of a double data type. Bear in mind that you can mix data types, so long as one number is a single number / scalar. This is also what the error is alluding to. Matrix multiplication of varaibles that are not floating point (double, single) is not supported in MATLAB so you need to make sure that your image data and your DFT matrices are the same type before applying your algorithm.
As such, simply do:
I = imread('sample.jpg');
R = dft1(double(I));
Minor Note
This code is quite clever, and it (by default) applies the 1D DFT to all columns of your image. The output will be a matrix of the same size as I where each column is the 1D DFT result of each column from I.
This is something to think about, but should you want to apply this to all rows of your image, you would simply transpose I before it goes into dft1 so that the rows become columns and you can operate on these new "columns". Once you're done, you simply have to transpose the result back so that you'll get your output from dft1 shaped such that the results are applied on a per row basis. Therefore:
I = imread('sample.jpg');
R = dft1(double(I.')).';
Hope this helps! Good luck!

How to multiply matrix of nxm with matrix nxmxp different dimensions in matlab

In my current analysis, I am trying to multiply a matrix (flm), of dimension nxm, with the inverse of a matrix nxmxp, and then use this result to multiply it by the inverse of the matrix (flm).
I was trying using the following code:
flm = repmat(Data.fm.flm(chan,:),[1 1 morder]); %chan -> is a vector 1by3
A = (flm(:,:,:)/A_inv(:,:,:))/flm(:,:,:);
However. due to the problem of dimensions, I am getting the following error message:
Error using ==> mrdivide
Inputs must be 2-D, or at least one
input must be scalar.
To compute elementwise RDIVIDE, use
RDIVIDE (./) instead.
I have no idea on how to proceed without using a for loop, so anyone as any suggestion?
I think you are looking for a way to conveniently multiply matrices when one is of higher dimensionality than the other. In that case you can use bxsfun to automatically 'expand' the smaller matrix.
x = rand(3,4);
y = rand(3,4,5);
bsxfun(#times,x,y)
It is quite simple, and very efficient.
Make sure to check out doc bsxfun for more examples.

Multiplication of Sparse matrix on Matlab

I am using Matlab R2011a. I have a matrix A which is of size 27*3355432. This matrix has a lot of zeros. I need to compute the comand eig(A'*A) but I can't even do the A'*A. I have tried using the sparse matrix B = sparse(A)and then computing B'*B but I get the error:
??? Error using ==> mtimes
Both logical inputs must be scalar.
To compute elementwise TIMES, use TIMES (.*) instead
Truth is I am not a Matlab expert. Is there a way to produce such a database?

How to convert logical sparse matrix into integer sparse matrix in MATLAB?

I have a matrix https://www.cise.ufl.edu/research/sparse/matrices/Hamm/add20.html
I want to consider it as adjacency matrix of corresponding graph, so I'm replacing every non zero element with 1:
A = A ~- 0
Now I want to calculate A*A
but I can't because
>> A*A
Error using *
Both logical inputs must be scalar.
To compute elementwise TIMES, use TIMES (.*) instead
and I for some reason I cannot just convert logical matrix to integer
>> uint(A)
Error using numerictype (line 172)
Invalid arguments (WordLength must be a scalar numeric value).
Error in fixdt (line 186)
embeddedType = numerictype( varargin{:} );
Error in uint (line 14)
DataType = fixdt(0,WordLength,0);
I could do it by converting matrix to full rank back and forth but this is impractical for my task.
Firstly, you would want to be using uint8 or similar, as opposed to uint - read the help files as to what the difference is, uint doesn't do what you think it does. However, according to this forum post, the only valid sparse data types are double or logical. You've got a logical matrix, but it would appear that sparse matrix multiplication is not defined for logical matrices. Thus, you must convert A to double form before you will be able to multiply it as you are trying to.
Alternatively, use A^2 instead - this will work with logical sparse matrices. Don't know why it's different.