(Error using vertcat) Dimensions of arrays being concatenated are not consistent

Hello all,
Here is my code. Some related .csv files are attached.
After running the code, I face this error:
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in Code_Yahya (line 105)
phia =[x;y;phix(:,a)';phiy(:,a)'];
>> both phix and phiy are of same sizes.
Can someone help me here ?
Thank you
%%% Tables are exported as vertices from STARCCM+
%%% ------------------------------------------------------------------%%%
clear; clc; close all;
f=dir('*.csv'); % finding all csv files
n_snapshots=size(f,1); % finding number of snapshots
for i=1:n_snapshots
fn=f(i).name; % Listing filenames
fid=fopen(fn, 'r'); % reading files
data = fscanf(fid, '%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f' , [11, inf]);
% exporting columns
x=data(9,:);
y=data(10,:);
%Ut(:,i) = data(:,1);
u(:,i) = data(:,3);
v(:,i) = data(:,2);
fclose(fid);
end
%--------------------------------------------------------------
u = permute(u,[2 1]);
%Split u into two snapshot sets
X1 = u(:,1:end-1);
X2 = u(:,2:end);
%SVD on X1
[Ux, Sx, Vx] = svd(X1,'econ');
%Compute DMD (phix are Eigen vectors)
r = 3; %truncate as 10 modes
%Reduce rank
Ux = Ux(:,1:r);
Sx = Sx(1:r,1:r);
Vx = Vx(:,1:r);
%Gets A_tilde
A_tildex = Ux'*X2*Vx/Sx;
%Compute A_tilde eigenvalues and eigenvectors
%[eVecs, Eigenvalues] = eig(A_tilde);
[Wx, eigsx] = eig(A_tildex);
%DMD modes
phix = X2*Vx/Sx*Wx; %Eigenvectors
figure
theta = (0:1:100)*2*pi/100;
plot(cos(theta),sin(theta),'k--') % plot unit circle
hold on, grid on
scatter(real(max(eigsx)),imag(max(eigsx)),'ok')
axis([-1.1 1.1 -1.1 1.1]);
%--------------------------------------------------------------
v = permute(v,[2 1]);
%Split u into two snapshot sets
Y1 = v(:,1:end-1);
Y2 = v(:,2:end);
%SVD on X1
[Uy, Sy, Vy] = svd(Y1,'econ');
%Compute DMD (phix are Eigen vectors)
r = 3; %truncate as 10 modes
%Reduce rank
Uy = Uy(:,1:r);
Sy = Sy(1:r,1:r);
Vy = Vy(:,1:r);
%Gets A_tilde
A_tildey = Uy'*Y2*Vy/Sy;
%Compute A_tilde eigenvalues and eigenvectors
%[eVecs, Eigenvalues] = eig(A_tilde);
[Wy, eigsy] = eig(A_tildey);
%DMD modes
phiy = Y2*Vy/Sy*Wy; %Eigenvectors
figure
theta = (0:1:100)*2*pi/100;
plot(cos(theta),sin(theta),'k--') % plot unit circle
hold on, grid on
scatter(real(max(eigsy)),imag(max(eigsy)),'ok')
axis([-1.1 1.1 -1.1 1.1]);
%--------------------------------------------------------------
Gridnumber = size(x, 2);
for j=1:r %n_snapshots
phinor = 0;
for i=1:r %Gridnumber
phinor = phinor + power(phix(i,j),2) + power(phiy(i,j),2);
%phinor = phinor + power(phix(i,j),2);
end
phinor = sqrt(phinor);
phix(:, j) = phix(:, j) / phinor;
phiy(:, j) = phiy(:, j) / phinor;
end
%Extracting modes
for a=1:r %n_snapshots
FilNamPhi=1000+a; %FilNamPhi=1000+a;
PhiOut = fopen([num2str(FilNamPhi), '.txt'], 'wt');
fprintf(PhiOut,'');
phia =[x;y;phix(:,a)';phiy(:,a)'];
fprintf(PhiOut, '%20.9f %20.9f %20.9f %20.9f\n',phia);
fclose(PhiOut);
end
%xlswrite('eigenvaluesX.xlsx',[real(max(eigsx))',imag(max(eigsx))'];
%xlswrite('eigenvaluesY.xlsx',[real(max(eigsy))',imag(max(eigsy))'];

Réponses (1)

Torsten
Torsten le 19 Juin 2025
Déplacé(e) : Torsten le 19 Juin 2025
x and y are of size 1x500, phix(:,a)' and phiy(:,a)' are of size 1x8. So either x and y had to be of size 1x8 or phix(:,a)' and phiy(:,a)' had to be of size 1x500 for that phia =[x;y;phix(:,a)';phiy(:,a)']; would make sense.

7 commentaires

Hello Mr. Torsten,
Thank you for your response. you are right. If sizes of x, y, phix, and phiy are same, it works.
But in my case, x and y are of fixed size and can't be modified (as inputs). phix and phiy are outputs.
In this scenario, how can I modify the specific line in the code to get my required output ? Kindly share your opinion.
Thank you
If you need to have a 1 x 500 followed by a 1 x 500 followed by a 1 x 8 and then another 1 x 8, then you will need to use a cell array.
Perhaps you want
[x.'; y.'; phix(:,a); phiy(:,a)]
which would be [500 x 1; 500 x 1; 8 x 1; 8 x 1] giving 1016 x 1 overall.
Hello Mr. Walter,
I have some more queries.
1) In the above code, when I fix the "r" value greater than 10 (for many csv files), it says:
Index in position 2 exceeds array bounds (must not exceed 10).
Error in DMD_Code_Yahya (line 33)
Ux = Ux(:,1:r);
I am kind of not sure, why it is ? r = number of energy modes extracted from the solution.
2) Added to that, when my "r" value is lesser or equal to 10, I get these warnings (for many csv files), can it be optimized ?
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.748731e-22.
> In DMD_Code_Yahya (line 37)
Line 37: A_tildex = Ux'*X2*Vx/Sx;
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.748731e-22.
> In DMD_Code_Yahya (line 43)
Line 43: phix = X2*Vx/Sx*Wx;
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 9.387731e-23.
> In DMD_Code_Yahya (line 73)
Line 73: A_tildey = Uy'*Y2*Vy/Sy;
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 9.387731e-23.
> In DMD_Code_Yahya (line 79)
Line 79: phiy = Y2*Vy/Sy*Wy;
Thank you once again ^^
The Ux you get from the command
%SVD on X1
[Ux, Sx, Vx] = svd(X1,'econ');
is a 8x8 matrix for your first .csv-file. Thus for r > 8, the command
Ux = Ux(:,1:r);
references columns in Ux that do not exist.
The other warnings tell you that you try to find inverses of matrices that are numerically singular (i.e. for which numerically a senseful inverse cannot be computed).
Example:
A = [1 0;0 1e-65];
B = [1 2;3 4];
B/A
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.000000e-65.
ans = 2×2
1.0e+65 * 0.0000 2.0000 0.0000 4.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
The number of rows of Ux is 20, but the number of columns of Ux is 10. Thus still r <= 10 is required.

Connectez-vous pour commenter.

Catégories

Commenté :

le 20 Juin 2025

Community Treasure Hunt

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

Start Hunting!

Translated by