need help with " Error using vertcat Dimensions of matrices being concatenated are not consistent"
이전 댓글 표시
Can anybody help me, cant understand why do i get this error when i run it?
i have attached my code.
thanks
댓글 수: 5
Jan
2015년 5월 19일
Please post the complete error message, such that the readers can focus on fixing the error instead of guessing where it happens.
Abhilash
2022년 12월 6일
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Z = table ([5:6;5],['M','F','M'],'VariableName',{'age','gender'},'RowName',{'A','B','C'})
Stephen23
2022년 12월 6일
"Dimensions of arrays being concatenated are not consistent."
[5:6;5]
^^^ 1x2, i.e. 1 row 2 columns
^ 1x1, i.e. 1 row 1 column
How do you expect to vertically concatenate arrays with different numbers of columns?
Also note that square brackets are a contcatenation operator, so your code
['M','F','M']
concatenates three scalar characters into one character vector, and is exactly equivalent to:
'MFM'
You probably intended to define a string column vector:
["M";"F";"M"]
h= 0.3; % Rayon de lissage
dx=0.3;
N =floor(1/dx);
x =0:dx:(pi/sqrt(3)); % Positions des particules
dt = 1/4;
t = 0:dt:1;
M = length(t) - 1;
% Initialisation de la solution approchée et de la matrice A
A = zeros(N, N);
b=zeros(N,1);
uapp=zeros(N,1);
%sol et cond exacte
u_exact = @(x) cos(sqrt(3)*x);
% Construction de la matrice A et du vecteur b
for i = 1:N
for j =N
r_ij = abs(x(i) - x(j));
if r_ij <= h
d2W_dx2 = second_derivative_monaghan_kernel(r_ij, h);
W_value = monaghan_kernel(r_ij, h);
A(i, j) = (u_exact(x(j))- u_exact(x(i))) * d2W_dx2;
b(i) = -3 * u_exact(x(j)) * W_value ;
end
end
end
u_app(1)=1;
u_app(N)=u_exact(pi/sqrt(3));
uapp = b\A;
uapp = [0; uapp];
% Affichage des résultats
plot(x , uapp, 'r');
hold on;
plot(x, u_exact(x), 'b--');
xlabel('x');
ylabel('u');
legend('Solution approchée (SPH)', 'Solution exacte');
title('Solution approchée et exacte par SPH');
grid on;
% Fonctions du noyau de Monaghan
function W_value =monaghan_kernel(r_ij, h)
C= (1/ h);
if r_ij < h
W_value =C*((2 / 3) - (r_ij / h)^2 +(1/2 *(r_ij / h)^3));
elseif r_ij<2*h
W_value =C*((1/6)*(2-(r_ij / h))^3);
else
W_value=0;
end
end
function d2W_dx2 = second_derivative_monaghan_kernel(r_ij, h)
C = (1 / h); % Corrected the denominator
if r_ij < h
d2W_dx2 = -2 * C * (1 - (3 * (r_ij / h)));
elseif r_ij < 2*h
d2W_dx2 = 2 * C * (2 - (r_ij / h)); % Removed extra characters
else
d2W_dx2 = 0;
end
end
Ihave a problem simultaning a numerical solution
I need some
Voss
2024년 4월 26일
@ABDO: Post a new question. Use this link: https://www.mathworks.com/matlabcentral/answers/questions/new/
답변 (5개)
Thorsten
2015년 5월 18일
When you vertically connect matrices using ; the matrices must have the same number of columns.
A = ones(10, 3);
B = zeros(20, 4);
C = [A; B]
You get
Error using vertcat
CAT arguments dimensions are not consistent.
Instead you can write
B = zeros(20, 3);
C = [A; B]; % works, A and B have same number of rows
댓글 수: 5
alon stuart
2015년 5월 19일
Your t has 51 values. Next you try to use t an index into your 3x3 matrix by
Qout_doot(t)
That does not work, because
1. t is not a positive integer value
2. t has more elements than Qout_doot.
You could use
t = linspace(0, 5, size(Qout_doot, 2));
plot(t, Qout_doot)
alon stuart
2015년 5월 20일
이동: DGM
2023년 3월 25일
alon stuart
2015년 5월 20일
이동: DGM
2023년 3월 25일
I guess that this fails:
t = 0:0.1:10;
...
Qin_doot = [4; 2*t; 10];
the sizes of the scalars 4 and 10 do not match with the size of t.
The description as text "ive get a 3X1 vector by multiplying ..." is less useful than posting the line of code you are talking of.
댓글 수: 3
alon stuart
2015년 5월 20일
John Amakali
2018년 5월 9일
편집: Walter Roberson
2018년 5월 9일
hi guys , i have the same problem... i have attached my code. someone help please.
Warning: Unsuccessful read: A timeout occurred before the Terminator was reached..
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in GUI>pushbutton1_Callback (line 144)
set(M,'Zdata',[Tdata; zeroIndex],'Cdata',[Tdata; Tdata]);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Walter Roberson
2018년 5월 9일
We do not know what size(Tdata) is or size(zeroIndex) .
The message about timeout suggests that you might be doing a serial read or read from arduino or raspberry, and that it is not returning as much data as you expect. If your Tdata is the actual received data and zeroIndex is built under the assumption that all of the requested data was received, then they could easily end up with incompatible sizes.
I've got another problem with using vertcat Dimensions of matrices being concatenated are not consistent.
I'm trying to write a code to make a transmission characteristic of the Gaussian filter from this
My code is:
lambdac=0.8; %mm
dx=0.001; %mm
alpha=sqrt(log(2)/pi);
disp(alpha);
x=(-lambdac:dx:lambdac);
S=(1/alpha*lambdac).*exp(-pi*(x/alpha*lambdac).^2);
S=S/sum(S);
figure(1)
plot(x,S)
xlabel('Odległość [mm]')
ylabel('Wzmocnienie')
xlim([-1 1])
%Przykład 5.2
m=size(S,1); %długość filtru gaussowskiego
l=8; %długość punktów pomiarowych na profilu
n=1/dx; %liczba punktów pomiarowych na profilu
S=[zeros(n/2-floor(m/2),1); S; zeros(n/ 2-floor(m/2)-1,1)]; %copied from %previous link
Sf=fft(S); %transformata Fouriera S
j=(2:1:floor (n/2)+1); %generuje falę o długości dla danych X
wave=n*dx./(j-1);
figure (2)
semilogx (wave, 100*abs(Sf(2:floor (n/2)+1,1)));
xlabel('Długość fali [mm]')
ylabel('Amplituda [%]')
hold off
I've tried also with
S=[zeros(n/2-floor(m/2)-1,1); S; zeros(n/ 2-floor(m/2)+1,1)];
but no result. After n=1/dx I've found:
a=zeros(n/2-floor(m/2),1);
size(a);
disp(size(a));
S;
size(S);
disp(size(S));
c=zeros(n/2-floor(m/2)+1,1);
size(c);
disp(size(c));
and result:
500 1
1 1601
501 1
Any idea how to solve it?
댓글 수: 1
So S has 1601 columns and you are trying to vertically concatenate it with two arrays, each of which has just one column. You need to ensure that the number of columns or rows is the same, e.g.:
R = n/2-floor(m/2);
S = [zeros(1,R-1), S, zeros(1,R+1)];
A N
2018년 2월 10일
편집: Walter Roberson
2018년 2월 10일
Great! Works!
After it I would like to do a FFT of S, but
My code:
Sf=fft(S); %transformata Fouriera
j=(2:1:floor(n/2)+1); %faa o danej długości
size(j);
disp(size(j));
wave=n*dx./(j-1);
size(wave);
disp(size(wave));
figure(2)
semilogx(wave,100*abs(Sf(2:floor(n/2)+1,1)))
xlabel('Długość fali [mm]')
ylabel('Amplituda [%]')
Command Windows shows as a result of those displays:
1 500
1 500
Index exceeds matrix dimensions.
Error in proba (line 61)
semilogx(wave,100*abs(Sf(2:floor(n/2)+1,1)));
Error in proba (line 65)
semilogx(wave,100*abs (2:floor(n/2)));"
Do you know how to solve it? Maybe my error is easy but I've been doing only spectral FFTs and would like to know more about examples like this (no Fs and spectral). In Help I found only spectral FFT. Line with semilog I copied from this book, and really hard to find out - is this wrong?
Edit:
Writing:
semilogx(wave,100*abs(Sf(2:floor(n/2)+1)));
impacts of this plot:
댓글 수: 7
Walter Roberson
2018년 2월 10일
At the command line command
dbstop if error
and run the code. When it stops, look at size(Sf) and look at the value of n and calculate floor(n/2)+1 and see if it is within the number of rows of Sf.
A N
2018년 2월 10일
Dimension of
Sf is 1 2601.
May I ,,lean" this matrix somehow or or make the others greater?
Walter Roberson
2018년 2월 10일
Okay so Sf is a row vector of length 2601. But what is n at the time that the problem occurs?
Walter Roberson
2018년 2월 11일
Sf(2:floor(n/2)+1,1) asks for row 2:501, column 1, but your Sf is a column vector that is 1 x 2601 not 2601 x 1.
Walter Roberson
2018년 2월 11일
The plot did not get attached.
MANYINGA MARC
2023년 3월 24일
0 개 추천
Bonjour la communauté. J'ai un code de calcul, Dan lequel je voudrais faire varier les éléments d'une matrice À, en fonction d'une variable temps t. Par exemple
t=0:10:60 A=[1 2*t 3 3*t 5 8 5 6 3] Affiche error using vertcat Besoin d'aide
댓글 수: 1
Walter Roberson
2023년 3월 24일
A=[1 2*t 3 3*t 5 8 5 6 3] would be an error if t is not a row vector.
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

