I'm complitily lost with that error: Error using horzcat Dimensions of matrices being concatenated are not consistent
2 views (last 30 days)
Show older comments
I have that code, to do some mathematic problem:
ER = 1.0;
EO = 8.8541e-12;
VO = 1.0;
AA = 0.001;
L = 1.0;
N = 20;
DELTA = L/N;
%--------------------------
% Matrix A
%--------------------------
I = 1:N;
Y = DELTA*(I-0.5);
for i = 1:N
for j = 1:N
if (i ~= j)
A(i,j) = DELTA/abs(Y(i)-Y(j));
else
A(i,j) = 2.0*log(DELTA/AA);
end
end
end
%---------------------
% Vector B
%-------------------
B = 4.0*pi*EO*ER*VO*ones(N,1);
C = inv(A);
RHO = C*B;
SUM = 0.0;
for I=1:N
SUM = SUM + RHO(I);
end
Q = SUM*DELTA;
diary c:magMetMom.txt
[EO,Q]
[ [1:N] ' Y' RHO ]
diary off
%------------------
% Plot
%----------------
plot(Y,RHO)
xlabel('y (cm)'), ylabel('rho_L (pC/m)')
ERROR Message:
"ans =
1.0e-11 *
0.8854 0.8579
Error using horzcat
Dimensions of matrices being concatenated are not consistent."
DOES anyone can help? Tks a lot !! =))))
0 Comments
Accepted Answer
dpb
on 5 Oct 2015
N = 20;
...
B = 4.0*pi*EO*ER*VO*ones(N,1); % size(B)--> [20,1]
C = inv(A); % size(A)--> [20,20]; ergo size(C)--> [20,20]
RHO = C*B; % RHO-->[20x20]*[20*1]==> size(RHO)=[20,1]
...
[ [1:N] ' Y' RHO ]
...
Error using horzcat
Dimensions of matrices being concatenated are not consistent."
So, from the above you're trying to concatenate
1x20 1x2(character) 20x1 into a row vector.
Won't work. You could do
[ [1:N] RHO.' ]
but Matlab won't let you(*) mix character data in with the numeric in a single vector; you'll have to use a cell array or convert all to string format to do that.
(*) Actually, the above would work for a specific definition of "working" but it won't be what you're wanting--
>> [1:3 ' y' 2]
ans =
y
>> whos ans
Name Size Bytes Class Attributes
ans 1x6 12 char
>>
NB: the numeric values were cast to character using their ASCII code equivalents which are non-printing characters. Similar gibberish will appear if the numeric values are floating point as in your case.
2 Comments
dpb
on 6 Oct 2015
No, diary really has nothing to do with it; it just happens that it in this particular case you were trying to output some results but the problem is in the attempt to concatenate the row and column vectors--it's a general rule irrespective of what you're doing or where or when.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!