필터 지우기
필터 지우기

How do tell Matlab to multiply a single array with one column of a matrix?

조회 수: 1 (최근 30일)
Jacob
Jacob 2013년 8월 12일
I have a one array which I need to multiply with a column of matrix. Based on my code below (After the ...%Call Substitute...line) Matlab takes the matrix as whole and thus dimensions wont match with those of the array. Any way I can go around this? This code is for solving a linear system with matrices A and B given...
clc
clear all
format long e
A = [2 1 3;2 6 8;6 8 18];
B = [1;3;5];
n = size(A);
tol = 1e-20;
er = 0;
%Call Gauss
for i=1:n
s(i)=abs(A(i,1));
for j=2:n
if abs(A(i,j))>s(i)
s(i)=abs(A(i,j));
end
end
end
%Call Eliminate
for k=1:n-1
%Call Pivot
p=k;
big=abs(A(k,k)/s(k));
for ii=k+1:n
dummy=abs(A(ii,k)/s(ii));
if dummy>big
big=dummy;
p=ii;
end
end
if p~=k
for jj=k:n
dummy=A(p,jj);
A(p,jj)=A(k,jj);
A(k,jj)=dummy;
end
dummy=B(p);
B(p)=B(k);
B(k)=dummy;
dummy=s(p);
s(p)=s(k);
s(k)=dummy;
end
%Back to Eliminate
if abs(A(k,k)/s(k))<tol
er=-1;
exit for loop
end
for i=k+1:n
factor=A(i,k)/A(k,k);
for j=k+1:n
A(i,j)=A(i,j)-factor*A(k,j);
end
B(i)=B(i)-factor*B(k);
end
end
if abs(A(n,n)/s(n))<tol
er=-1;
end
if er~=-1
%Call Substitute
X(n)=B(n)/A(n,n)
for i=n-1:-1:1
sum=0;
for j=i+1:n
sum=sum+A(i,j)*X(j);
end
X(n)=(B(n)-sum)/A(n,n)
end
end
Here is the what Matlab tells me:
Error using /
Matrix dimensions must agree.
Error in GaussEliminationwithPivoting (line 69)
X(n)=B(n)/A(n,n)

답변 (1개)

Sean de Wolski
Sean de Wolski 2013년 8월 12일
Not sure what you want, but it sounds like you'll either want element by element division, i.e:
B(n)./A(n,n)
Or bsxfun to do the scalar expansion:
bsxfun(@rdivide,B(n),A(n,n));

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by