Error using * Inner matrix dimensions must agree - matlab

I'm trying to calculate some functions in matlab and I'm getting this error:
Error using *
Inner matrix dimensions must agree.
Error in set1 (line 11)
x = (Ac + m)*cos(2*pi*fc*t);
but I don't use any kind of matrix in my code. What is the problem about?
Here is my code:
fs = 10000;
Ts = 1/fs;
t = (0:Ts:10);
m = cos(2*pi*t);
plot(t,m);
figure;
Ac = 2;
fc = 500;
x = (Ac + m)*cos(2*pi*fc*t);
plot(t,x);
figure;

Try elementwise multiplication by adding a dot before *:
x = (Ac + m).*cos(2*pi*fc*t);

Related

1D finite element method in the Hermite basis (P3C1) - Problem of solution calculation

I am currently working on solving the problem $-\alpha u'' + \beta u = f$ with Neumann conditions on the edge, with the finite element method in MATLAB.
I managed to set up a code that works for P1 and P2 Lagragne finite elements (i.e: linear and quadratic) and the results are good!
I am trying to implement the finite element method using the Hermite basis. This basis is defined by the following basis functions and derivatives:
syms x
phi(x) = [2*x^3-3*x^2+1,-2*x^3+3*x^2,x^3-2*x^2+x,x^3-x^2]
% Derivative
dphi = [6*x.^2-6*x,-6*x.^2+6*x,3*x^2-4*x+1,3*x^2-2*x]
The problem with the following code is that the solution vector u is not good. I know that there must be a problem in the S and F element matrix calculation loop, but I can't see where even though I've been trying to make changes for a week.
Can you give me your opinion? Hopefully someone can see my error.
Thanks a lot,
% -alpha*u'' + beta*u = f
% u'(a) = bd1, u'(b) = bd2;
a = 0;
b = 1;
f = #(x) (1);
alpha = 1;
beta = 1;
% Neuamnn boundary conditions
bn1 = 1;
bn2 = 0;
syms ue(x)
DE = -alpha*diff(ue,x,2) + beta*ue == f;
du = diff(ue,x);
BC = [du(a)==bn1, du(b)==bn2];
ue = dsolve(DE, BC);
figure
fplot(ue,[a,b], 'r', 'LineWidth',2)
N = 2;
nnod = N*(2+2); % Number of nodes
neq = nnod*1; % Number of equations, one degree of freedom per node
xnod = linspace(a,b,nnod);
nodes = [(1:3:nnod-3)', (2:3:nnod-2)', (3:3:nnod-1)', (4:3:nnod)'];
phi = #(xi)[2*xi.^3-3*xi.^2+1,2*xi.^3+3*xi.^2,xi.^3-2*xi.^2+xi,xi.^3-xi.^2];
dphi = #(xi)[6*xi.^2-6*xi,-6*xi.^2+6*xi,3*xi^2-4*xi+1,3*xi^2-2*xi];
% Here, just calculate the integral using gauss quadrature..
order = 5;
[gp, gw] = gauss(order, 0, 1);
S = zeros(neq,neq);
M = S;
F = zeros(neq,1);
for iel = 1:N
%disp(iel)
inod = nodes(iel,:);
xc = xnod(inod);
h = xc(end)-xc(1);
Se = zeros(4,4);
Me = Se;
fe = zeros(4,1);
for ig = 1:length(gp)
xi = gp(ig);
iw = gw(ig);
Se = Se + dphi(xi)'*dphi(xi)*1/h*1*iw;
Me = Me + phi(xi)'*phi(xi)*h*1*iw;
x = phi(xi)*xc';
fe = fe + phi(xi)' * f(x) * h * 1 * iw;
end
% Assembly
S(inod,inod) = S(inod, inod) + Se;
M(inod,inod) = M(inod, inod) + Me;
F(inod) = F(inod) + fe;
end
S = alpha*S + beta*M;
g = zeros(neq,1);
g(1) = -alpha*bn1;
g(end) = alpha*bn2;
alldofs = 1:neq;
u = zeros(neq,1); %Pre-allocate
F = F + g;
u(alldofs) = S(alldofs,alldofs)\F(alldofs)
Warning: Matrix is singular to working precision.
u = 8×1
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
figure
fplot(ue,[a,b], 'r', 'LineWidth',2)
hold on
plot(xnod, u, 'bo')
for iel = 1:N
inod = nodes(iel,:);
xc = xnod(inod);
U = u(inod);
xi = linspace(0,1,100)';
Ue = phi(xi)*U;
Xe = phi(xi)*xc';
plot(Xe,Ue,'b -')
end
% Gauss function for calculate the integral
function [x, w, A] = gauss(n, a, b)
n = 1:(n - 1);
beta = 1 ./ sqrt(4 - 1 ./ (n .* n));
J = diag(beta, 1) + diag(beta, -1);
[V, D] = eig(J);
x = diag(D);
A = b - a;
w = V(1, :) .* V(1, :);
w = w';
x=x';
end
You can find the same post under MATLAB site for syntax highlighting.
Thanks
I tried to read courses, search in different documentation and modify my code without success.

Octave: Kmeans clustering not working on an image matrix

I have tried the following code.
img=imread("test1.jpg");
gimg=rgb2gray(img);
imshow(gimg);
bw = gimg < 255;
L = bwlabel(bw);
imshow(label2rgb(L, #jet, [.7 .7 .7]))
s = regionprops(L, 'PixelIdxList', 'PixelList');
s(1).PixelList(1:4, :)
idx = s(1).PixelIdxList;
sum_region1 = sum(gimg(idx));
x = s(1).PixelList(:, 1);
y = s(1).PixelList(:, 2);
xbar = sum(x .* double(gimg(idx))) / sum_region1
ybar = sum(y .* double(gimg(idx))) / sum_region1
hold on
for k = 1:numel(s)
idx = s(k).PixelIdxList;
pixel_values = double(gimg(idx));
sum_pixel_values = sum(pixel_values);
x = s(k).PixelList(:, 1);
y = s(k).PixelList(:, 2);
xbar = sum(x .* pixel_values) / sum_pixel_values;
ybar = sum(y .* pixel_values) / sum_pixel_values;
plot(xbar, ybar, '*')
end
hold off
a=round(xbar)-90;
b=round(xbar)+90;
c=round(ybar)-90;
d=round(ybar)+90;
roi=gimg(a:b,c:d);
imshow(roi);
roi(:,:,2)=0;
roi(:,:,3)=0;
se = strel('cube',20);
closeBW = imclose(roi,se);
figure
imshow(closeBW);
de=rgb2gray(closeBW);
ed=edge(de,"canny");
imshow(ed);
j=kmeans(ed,3);
What i did was take an image and extracted its grayscale.I concentrated on the part of image which has very high intensity.I then took the red component of the image and then applied closing operation on the resultant image.After that I applied edge detection using canny method.Then I tried to use kmeans on result of edge detection.
I get an error saying kmeans requires real matrix.
help would appreciated.
edge in MATLAB / Octave returns a binary / logical matrix. kmeans requires that the input be a double or single matrix.
Therefore, simply cast ed to double and continue:
ed=edge(de,"canny");
imshow(ed);
ed = double(ed); %// Change
j=kmeans(ed,3);

How do you correct the error 'Index exceeds matrix dimensions' in matlab

My code:
A = importdata('befootball.xls','Sheet1','E2:L11');
w1 = 0.01;
w2 = 0.1;
w3 = 1;
w4 = 1;
a = A(:,1);
sg = A(:,2);
rg = A(:,3);
c = A(:,4);
p = A(:,5);
for n = 1:10
f1(n)=w1(n)*c(n) + (w2(n)*p(n)) + (w3(n)*a(n));
end
Error:
Index exceeds matrix dimensions.
Error in fitnessfunctionoutputt (line 33)
sg = A(:,3);
The same error also comes for rg and subsequently all the variables apart from 'a'.
Is there anyway of solving this?
Thanks in advance

blockwise image rotation in matlab

I am working on ridge frequency estimation for which I have to obtain an blockwise oriented image. I have developed the code but unable to remove an error.
the process goes like this. Input image is G(m,n) and theta(m,n) is an image containing theta. Input image G is divide into w*w block (w = 17) and for each block centred at pixel(i,j) compute an oriented window of size l*w (33*17). For each block we have to compute the x signature as
x[0],x[1],x[2].....x[l-1]...
x[k] = 1/w * summation(d=0 to w-1)G(u,v)
u = i+ (d-(w/2))cos(theta(i,j))+(k-(l/2))sin(theta(i,j))
v = i+ (d-(w/2))sin(theta(i,j))+((l/2)-k)cos(theta(i,j))
here is my code
% To compute ridge frequency
theta_image; theta image as input
im = imread('cameraman.tif');
[m,n] = size(im);
l = (2*w-1);
w = 17;
ww = floor(w/2);
for i1 = 9:w:m
for j1 = 9:w:n
G = im(i1-ww:i1+ww,j1-ww:j1+ww);
O = theta(i1-ww:i1+ww,j1-ww:j1+ww);
G = padarray(G,[10 10],'both'); not sure its correct?
for k = 0:l-1
for d = 0:w-1
cnst_1 = (d-floor(w/2));
cnst_2 = (k-floor(l/2));
cnst_3 = (floor((l/2)-k);
u = i1+(cnst_1.*cos(O(i1,j1)) + cnst_2.*sin(O(i1,j1)));
v = j1+(cnst_1.*sin(O(i1,j1)) + cnst_3.*cos(O(i1,j1)));
u = fix(u);
v = fix(v);
store(k,d) = G(u,v);
x(k) = (sum(G(u,v)))/w; not sure will it be stored block-wise?
end;
end;
end;
end;
the error is
Attempted to access G(22,0); index must be a positive integer or logical.
Error in ridge_frequency (line 76)
store(k,d) = G(u,v);

Cubic spline interpolation of function

I am trying to interpolate the following function using the MATLAB function spline,
at equidistant points xi = i./n, i = 0,1,...,n, and for n = 2^j, j = 4,5,...,14.
For each calculation, I record the maximum error at the points x = 0:0.001:1 and plot these errors against n using a loglog plot.
Below is the code,
index=1
for j = 4:1:14;
n = 2^j;
i = 0:1:n;
xi = i./n;
yi = ((exp(3*xi))*sin(200.*(xi.^2))) ./(1+20.*(xi.^2));
x = 0:.001:1;
ye = ((exp(3*x))*sin(200*x.^2)) ./(1+20*x.^2);
yp = spline(x,xi,yi);
err = ye - yp;
merr(index) = max(err);
index = index+1;
end
n1 = 10:10:170;
loglog(n1, merr,'.')
xlabel('n');
ylabel('errors');
title('Cubic Splines');
but when I run the code, I got the following error:
Error using * Inner matrix dimensions must agree.
Error in (line 9) yi = ((exp(3*xi))sin(200.(xi.^2)))
./(1+20.*(xi.^2));
I'm just starting learning MatLab, can anyone help?
You want element-wise multiplication (.*) for the following part of code:
yi = ((exp(3*xi))*sin(200.*(xi.^2))) ./(1+20.*(xi.^2));
should be
yi = ((exp(3*xi)).*sin(200.*(xi.^2))) ./(1+20.*(xi.^2));
The same issue is present for the computation of ye.
When you use mtimes (*), MATLAB tries to do matrix multiplication, which in your case (1-by-n times 1-by-n) is invalid.
Then you will run into a problem with your spline command. Change it to yp = spline(xi,yi,x); so that the values at which you want to interploate (x) are the last argument.