# need help with " Error using vertcat Dimensions of matrices being concatenated are not consistent"

693 ビュー (過去 30 日間)
alon stuart 2015 年 5 月 18 日
コメント済み: Stephen23 2022 年 12 月 6 日
Can anybody help me, cant understand why do i get this error when i run it?
i have attached my code.
thanks
##### 5 件のコメント表示非表示 4 件の古いコメント
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"]

サインインしてコメントする。

### 回答 (7 件)

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.
B = zeros(20, 3);
C = [A; B]; % works, A and B have same number of rows
##### 1 件のコメント表示非表示 なし
alon stuart 2015 年 5 月 19 日
thanks for answering. i understand what you have suggested but i don't think this is the case. ive get a 3X1 vector by multiplying a 3 by 3 matrix with a 3X1 vector then i try to plot this vector by attaching a timeline vector from 0 to 5 sec. any other suggestions ?

サインインしてコメントする。

Jan 2015 年 5 月 19 日

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 件のコメント表示非表示 2 件の古いコメント
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.

サインインしてコメントする。

Thorsten 2015 年 5 月 20 日

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)
##### 0 件のコメント表示非表示 -1 件の古いコメント

サインインしてコメントする。

alon stuart 2015 年 5 月 20 日
thanks thorsten but its still doesnt work
##### 1 件のコメント表示非表示 なし
Thorsten 2015 年 5 月 20 日

Ok, so what goes wrong? My example should work for Qout_doot of any size without a Matlab error. But the result may not be what you are looking for? Or what exactly doesn't work?

サインインしてコメントする。

alon stuart 2015 年 5 月 20 日
iam not sure what is the problem exactly
the first code work perfectly so why the seconed one doesnt work? (first code): "
P0 =
cos(t1 + t2)/2 + (4*cos(t1))/5
sin(t1 + t2)/2 + (4*sin(t1))/5
4/5 - d3
1
t1=4*t; t2=t^2+3; d3=10*t;
P=subs(P0)
P=P(1:3);
P = matlabFunction(P);
t = 0:0.1:10;
plot(t,P(t));
"
(second code):"
syms t1 t2 d3 t4 t
F=P0(1:3);
J = [diff(F(1),t1) diff(F(1),t2) diff(F(1),d3); diff(F(2),t1) diff(F(2),t2) diff(F(2),d3); diff(F(3),t1) diff(F(3),t2) diff(F(3),d3)]
Qin_doot=[4;2*t;10];
Qout_doot=simplify(J*Qin_doot)
t1=4*t; t2=t^2+3; d3=10*t; t4=0;
Qout_doot=subs(Qout_doot)
Qout_doot =matlabFunction (Qout_doot);
t = 0:0.1:5;
plot(t,Qout_doot(t));
"
thanks again
##### 0 件のコメント表示非表示 -1 件の古いコメント

サインインしてコメントする。

A N 2018 年 2 月 10 日

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])
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 件のコメント表示非表示 なし
Stephen23 2018 年 2 月 10 日

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 日

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 件のコメント表示非表示 6 件の古いコメント
Walter Roberson 2018 年 2 月 11 日
The plot did not get attached.

サインインしてコメントする。

### カテゴリ

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!

Translated by