# how to fix this error ?

10 visualizzazioni (ultimi 30 giorni)
laila il 17 Ott 2023
Modificato: dpb il 18 Ott 2023
how to fix this error?
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number
of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise
multiplication.
function [X]=f_max_fn(Adj)
N = size(Adj,1);
k = 1;
X0 = randn(N,k);
X0 = orth(X0);
opts.record = 0;
opts.mxitr = 1000;
opts.xtol = 1e-7;
opts.gtol = 1e-7;
opts.ftol = 1e-10;
out.tau = 1e-7;
[X, out]= OptStiefelGBB(X0, @funDV, opts, Adj);
function [F, G] = funDV(x, Adj)
NN = length(x);
grad = zeros(NN,1);
for j = 1:NN
grad(j)= 2 * ( Adj(:,j)' * max(x(j) - x,0) - Adj(j,:) * max(x - x(j),0) );
end
G = - grad;
F = -sum(sum(Adj .* max(repmat(x',NN,1) - repmat(x,1,NN),0).^2));
end
end
Error in f_max_fn/funDV (line 23)
grad(j)= 2 * ( Adj(:,j)' * max(x(j) - x,0) - Adj(j,:) * max(x - x(j),0) );
Error in OptStiefelGBB (line 112)
[F, G] = feval(fun, X , varargin{:}); out.nfe = 1;
if isempty(X)
error('input X is an empty matrix');
else
[n, k] = size(X);
end
if nargin < 2; error('[X, out]= OptStiefelGBB(X0, @fun, opts)'); end
if nargin < 3; opts = []; end
if ~isfield(opts, 'X0'); opts.X0 = []; end
if ~isfield(opts, 'xtol'); opts.xtol = 1e-6; end
if ~isfield(opts, 'gtol'); opts.gtol = 1e-6; end
if ~isfield(opts, 'ftol'); opts.ftol = 1e-12; end
% parameters for control the linear approximation in line search,
if ~isfield(opts, 'tau'); opts.tau = 1e-3; end
if ~isfield(opts, 'rhols'); opts.rhols = 1e-4; end
if ~isfield(opts, 'eta'); opts.eta = 0.1; end
if ~isfield(opts, 'retr'); opts.retr = 0; end
if ~isfield(opts, 'gamma'); opts.gamma = 0.85; end
if ~isfield(opts, 'STPEPS'); opts.STPEPS = 1e-10; end
if ~isfield(opts, 'nt'); opts.nt = 5; end
if ~isfield(opts, 'mxitr'); opts.mxitr = 1000; end
if ~isfield(opts, 'record'); opts.record = 0; end
if ~isfield(opts, 'tiny'); opts.tiny = 1e-13; end
%-------------------------------------------------------------------------------
% copy parameters
xtol = opts.xtol;
gtol = opts.gtol;
ftol = opts.ftol;
rhols = opts.rhols;
STPEPS = opts.STPEPS;
eta = opts.eta;
gamma = opts.gamma;
retr = opts.retr;
record = opts.record;
nt = opts.nt;
crit = ones(nt, 3);
tiny = opts.tiny;
%-------------------------------------------------------------------------------
%% Initial function value and gradient
% prepare for iterations
[F, G] = feval(fun, X , varargin{:}); out.nfe = 1;
GX = G'*X;
if retr == 1
invH = true; if k < n/2; invH = false; eye2k = eye(2*k); end
if invH
GXT = G*X'; H = 0.5*(GXT - GXT'); RX = H*X;
else
U = [G, X]; V = [X, -G]; VU = V'*U;
%U = [G, X]; VU = [GX', X'*X; -(G'*G), -GX];
%VX = VU(:,k+1:end); %VX = V'*X;
VX = V'*X;
end
end
dtX = G - X*GX; nrmG = norm(dtX, 'fro');
Q = 1; Cval = F; tau = opts.tau;
%% Print iteration header if debug == 1
if (opts.record == 1)
fid = 1;
fprintf(fid, '----------- Gradient Method with Line search ----------- \n');
fprintf(fid, '%4s %8s %8s %10s %10s\n', 'Iter', 'tau', 'F(X)', 'nrmG', 'XDiff');
%fprintf(fid, '%4d \t %3.2e \t %3.2e \t %5d \t %5d \t %6d \n', 0, 0, F, 0, 0, 0);
end
%% main iteration
for itr = 1 : opts.mxitr
XP = X; FP = F; GP = G; dtXP = dtX;
% scale step size
nls = 1; deriv = rhols*nrmG^2; %deriv
while 1
% calculate G, F,
if retr == 1
if invH
[X, infX] = linsolve(eye(n) + tau*H, XP - tau*RX);
else
[aa, infR] = linsolve(eye2k + (0.5*tau)*VU, VX);
X = XP - U*(tau*aa);
end
else
[X, RR] = myQR(XP - tau*dtX, k);
end
if norm(X'*X - eye(k),'fro') > tiny; X = myQR(X,k); end
[F,G] = feval(fun, X, varargin{:});
out.nfe = out.nfe + 1;
if F <= Cval - tau*deriv || nls >= 5
break;
end
tau = eta*tau; nls = nls+1;
end
GX = G'*X;
if retr == 1
if invH
GXT = G*X'; H = 0.5*(GXT - GXT'); RX = H*X;
else
U = [G, X]; V = [X, -G]; VU = V'*U;
%U = [G, X]; VU = [GX', X'*X; -(G'*G), -GX];
%VX = VU(:,k+1:end); % VX = V'*X;
VX = V'*X;
end
end
dtX = G - X*GX; nrmG = norm(dtX, 'fro');
S = X - XP; XDiff = norm(S,'fro')/sqrt(n);
tau = opts.tau; FDiff = abs(FP-F)/(abs(FP)+1);
%Y = G - GP; SY = abs(iprod(S,Y));
Y = dtX - dtXP; SY = abs(iprod(S,Y));
if mod(itr,2)==0; tau = (norm(S,'fro')^2)/SY;
else tau = SY/(norm(Y,'fro')^2); end
tau = max(min(tau, 1e20), 1e-20);
if (record >= 1)
fprintf('%4d %3.2e %4.3e %3.2e %3.2e %3.2e %2d\n', ...
itr, tau, F, nrmG, XDiff, FDiff, nls);
%fprintf('%4d %3.2e %4.3e %3.2e %3.2e (%3.2e, %3.2e)\n', ...
% itr, tau, F, nrmG, XDiff, alpha1, alpha2);
end
crit(itr,:) = [nrmG, XDiff, FDiff];
mcrit = mean(crit(itr-min(nt,itr)+1:itr, :),1);
%if (XDiff < xtol && nrmG < gtol ) || FDiff < ftol
%if (XDiff < xtol || nrmG < gtol ) || FDiff < ftol
%if ( XDiff < xtol && FDiff < ftol ) || nrmG < gtol
%if ( XDiff < xtol || FDiff < ftol ) || nrmG < gtol
%if any(mcrit < [gtol, xtol, ftol])
if ( XDiff < xtol && FDiff < ftol ) || nrmG < gtol || all(mcrit(2:3) < 10*[xtol, ftol])
out.msg = 'converge';
break;
end
Qp = Q; Q = gamma*Qp + 1; Cval = (gamma*Qp*Cval + F)/Q;
end
if itr >= opts.mxitr
out.msg = 'exceed max iteration';
end
out.feasi = norm(X'*X-eye(k),'fro');
if out.feasi > 1e-13
%X = MGramSchmidt(X);
X = myQR(X,k);
[F,G] = feval(fun, X, varargin{:});
out.nfe = out.nfe + 1;
out.feasi = norm(X'*X-eye(k),'fro');
end
out.nrmG = nrmG;
out.fval = F;
out.itr = itr;
end
function a = iprod(x,y)
%a = real(sum(sum(x.*y)));
a = real(sum(sum(conj(x).*y)));
end
function [Q, RR] = myQR(XX,k)
[Q, RR] = qr(XX, 0);
diagRR = sign(diag(RR)); ndr = diagRR < 0;
if nnz(ndr) > 0
Q = Q*spdiags(diagRR,0,k,k);
%Q(:,ndr) = Q(:,ndr)*(-1);
end
end
Error in f_max_fn (line 18)
[X, out]= OptStiefelGBB(X0, @funDV, opts, Adj);
Error in DGFT (line 14)
temp(:,itr) = f_max_fn(A); % finds a stationary solution for the problem maximize_{||x|| = 1} DV(x)
A = [0,1,0,0,0;0,0,1,0,0;0,0,0,1,0;0,0,0,0,1;0,0,0,0,1;0,0,1,0,1]; % The given adjacency matrix
num_init = 10; % number of initializations for finding the maximum frequency
[DGFT_frequencies,DGFT_basis] = DGFT(A,10) % outputs the frequencies along with the basis
Error in Example_to_run_DGFT (line 5)
[DGFT_frequencies,DGFT_basis] = DGFT(A,10) % outputs the frequencies along with the basis
##### 2 CommentiMostra NessunoNascondi Nessuno
dpb il 17 Ott 2023
Modificato: dpb il 17 Ott 2023
Way big amount of code to try to wade through but problem seems to be in
grad(j)= 2 * ( Adj(:,j)' * max(x(j) - x,0) - Adj(j,:) * max(x - x(j),0) );
Set a breakpoint and inspect the dimensions and orientations of the components of the matrix multiplications -- Adj(:,j)' will be a row vector in the first whereas Adj(j,:) in the second will be a column vector -- if the sizes are commensurate for matrix multiplicaion with max(x(j)-x,0), then one or the other of those is wrong orientation. It's quite possible it's just a missing transpose operator on the second (although unless Adj is complex, use .' instead).
Or, of course, it could be that x is a vector and it's not supposed to be matrix multiply at all but another elementwise operation so "*" should have been ".*" -- but there would still be a mismatch between orientation between the two pieces.
We simply don't know without a lot of study what is intended as there's no doc or explanation of what the code is intended to do.
laila il 18 Ott 2023
Thanks you for your answer.
the problem is when I change the length of this matrix to >5 or <5
A = [0,1,0,0,0;0,0,1,0,0;0,0,0,1,0;0,0,0,0,1;0,0,0,0,1];

Accedi per commentare.

### Risposte (2)

Sulaymon Eshkabilov il 17 Ott 2023
Check the size of Adj(:, j)' vs. Adj(j,:) VS X
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
laila il 18 Ott 2023
Thanks you for your answer.
the problem is when I change the length of this matrix to >5 or <5
A = [0,1,0,0,0;0,0,1,0,0;0,0,0,1,0;0,0,0,0,1;0,0,0,0,1];

Accedi per commentare.

dpb il 18 Ott 2023
Modificato: dpb il 18 Ott 2023
Well, you'll have to understand the role of the adjacency matrix in Algorithm 2 in the following paper:
R. Shafipour, A. Khodabakhsh, G. Mateos and E. Nikolova, IEEE Trans. Signal Process."A Directed Graph Fourier Transform With Spread Frequency Components",2019, V67, N4, pp946-960
I have no idea what that entails but it is mandatory that A be square
A = [0,1,0,0,0;0,0,1,0,0;0,0,0,1,0;0,0,0,0,1;0,0,0,0,1]
A = 5×5
0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1
for the algorithm to run -- that's the only way that A(:,j)' and A(j,:) can have the same length to be commensurate with x in the line that errors.
What some other adjacency matrix of some other size should look like is all going to be dependent upon what its role is in the reference algorithm and we have no knowledge a priori of that; understanding that is your task if you have reason to be changing it; that is outside the scope of MATLAB Answers.
Now, when you change the size of A, then x will have to change to match or you'll have a different size mismatch error crop up. Whether that will happen automagically somehow inside the code logic is another question to answer.
In short, to use the code for anything except for what is specifically provided for, you'll have to understand the implications of making any changes; the original author didn't provide much documentation inside the code to aid in its understanding so you'll have to read the paper and then figure out how the code works to implement it in order to be able to make changes.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su File Operations in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by