I'm trying get A^T * A.
but if I try it, I get the 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.
so I did A^T .* A and it gave me the error
Matrix dimensions must agree.
I don't get it? shouldn't ATA work fine?
A is
A = imread('C:\Users\user1\Documents\MATLAB\5\assignment05_A.jpg');
A = rgb2gray(A);
before trying the multiplication
How do i get a A^T * A matrix??
You can convert to an array of double precision with double then plot with imshow;
A = double(rgb2gray(A));
ATA = A'*A;
imshow(ATA,[]);
Related
I tried to write the following in Matlab
x=[0:0.1:1];
f=(x.^2)*sin(pi*x.^2);
But I was getting an error:
Error using * Inner matrix dimensions must agree.
Try
f=(x.^2) * sin(pi*x.^2)'
This yields a scalar.
f=(x.^2)' * sin(pi*x.^2)
becomes a matrix and
f=(x.^2)' .* sin(pi*x.^2)
a vector.
When I try to plot a function h in MATLAB, using a variable omega which is defined as its own function, I get an Inner matrix dimensions must agree, error using _*_ response from the console.
The function works when I use a + between the seperate function-components of h; It does not work when I try multiplying the two inner functions in h, which is, from what I guess, what causes the matrix dim error.
function h = freqp(omega)
k = (1:1024-1);
hh = (1:1024-1);
omega = zeros(length(k),1);
omega = (k-1)*((2*pi)/1024);
hh = 2*exp((-3j)*omega)*cos(omega); % This works for ...omega) + cos(...
% but not for ...omega) * cos(, why?
y = fft(hh);
stem(real(y), omega);
How can I solve this? I read the info on mathworks but it only gives a solution for e.g. loading a file. Any help would be greatly appreciated!
Since Omega is a vector, the addition works. But multiplication of two vectors will result as a matrix. You can modify
hh = 2*exp((-3j)*omega)*cos(omega);
as
hh = 2*exp((-3j)*omega)*(cos(omega))';
or looking for element wise multiplication,
use
hh = 2*exp((-3j)*omega).*cos(omega);
The part exp((-3j)*omega worked fine because -3j is a complex scalar and omega a vector. Thus, MATLAB multiplies each element of omega with -3i. However, that result is a vector itself. Also cos(omega) is a vector, and both are row vectors.
In this case, with two vectors, the *-operator means dot product but that would be calculated between a column vector and a row vector, not two row vectors. So, [1 2 3] * [4 5 6] will raise the same error you are reporting, but [1 2 3] * [4 5 6]' yields 32.
From invoking fft on hh your code looks, however, as if you never intended to calculate a dot product (a scalar) but instead were looking for element-wise multiplication. The operator for element-wise multiplication is .*, such that your expression would be instead
hh = 2*exp((-3j)*omega).*cos(omega);
I am trying to use a function in Matlab which will give me the following equation:
The x and a values are in two matrices. I have tried almost everything, but cannot get the correct answer. Anyone who can help??
Thanks
Assuming A and X are vectors of size n x 1, you could construct that expression by writing transpose(X) * (sqrt(A * transpose(A)) .* (ones(n) - eye(n))) * X.
Another way to do this is
a = sqrt(ain); % ain is your input column vector
A = a*a.';
A = A-diag(diag(A));
aresult = x.'*A*x % x is your (other) input column vector
I have an array of numbers and I want to integrate each column in the array seperatley, and in the end get back an array of numbers after the integration.
I tried "trapz" function but I get a single value, how can i do what I want above?
Here's my code:
t=-1:0.001:1;
x1=100*sinc(100*t);
x2= 100*(sinc(100*t)).^2;
W= -2000*pi:2*pi:2000*pi;
T=-1:0.001:1;
u=x1.*exp(-1i.*W.*t);
v=x2.*exp(-1i.*W.*t);
X11= trapz(t,u);
X22= trapz(t,v);
Thanks in advance.
If I'm following you correctly, you need u and v to be matrices. For that purpose, you have to resolve two issues in your code:
the ω⋅t product should be a matrix rather than a vector. For that purpose, you need to use matrix multiplication W.' * t (note the added transpose!) and not element-wise multiplication (.*). This produces all the necessary combinations of ω⋅t required for the transform.
In a similar fashion, you need to multiply x by exp(-iωt) column-wise. Use bsxfun instead of the element-wise multiplication, like so:
u = bsxfun(#times, x1(:), exp(-i * W.' * t));
The same applies for v.
Since you're using the same exp(-i * W.' * t) both for u and for v, I suggest computing it once and storing it in a variable:
E = exp(-i * W' * t);
u = bsxfun(#times, x1(:), E);
v = bsxfun(#times, x2(:), E);
Following this fix, trapz should produce the desired results now, i.e. X11 and X12 should really be the Fourier Transform applied on x1 and x2, respectively.
It's from this question。
Why the two solutions doesn't work, though it looks very valid for me:
>> t = -pi:0.1:pi;
>> r = ((sin(t)*sqrt(cos(t)))*(sin(t) + (7/5))^(-1)) - 2*sin(t) + 2 ;
??? Error using ==> mtimes
Inner matrix dimensions must agree.
>> t = -pi:0.1:pi;
>> r = ((sin(t).*sqrt(cos(t))).*(sin(t) + (7/5)).^(-1)) - 2*sin(t) + 2 ;
>> plot(r,t)
??? Error using ==> plot
Vectors must be the same lengths.
What's wrong with the above?
The * operator is the matrix multiplication operator, which requires its operands to have matching inner matrix dimensions. The .* operator is the element-wise multiplication operator, which requires its operands to have the same size (or for one to be a scalar) so it can perform multiplication on each matching pair of elements. See this link for more detail.
Also, I don't get the plotting error you do when I run the second solution. I just get this warning:
Warning: Imaginary parts of complex X and/or Y arguments ignored