I AM NOT UNDERSTANDING THE ERROR THIS IS MY CODE ERROR:-Error using horzcat Dimensions of matrices being concatenated are not consistent. Error in personIden​tifierImag​eReader (line 21) image_data1 = [image_data1, last_image];

213 views (last 30 days)
clear all
close all
% Training Data
% Train File
chosen_folder = 'C:\Users\123\Music\face-recognition-master\pic';
filename_list = dir(chosen_folder); %this allows us to read all filenames in a folder and save it in a structure
xtrain = [];
y = [];
% x1 = zeros(960, 624);
image_data1 = [];
N = length(filename_list); %the length of the filename structure
for n = 3:N
filename = fullfile(chosen_folder,filename_list(n,1).name); %choosing the name of the last file
last_image = imread(filename,'pgm'); %reading the last file
image_data1 = [image_data1, last_image];
%figure(1),image(last_image);colormap(gray) %drawing the image,just to understand image files
[M N2 ] = size(last_image);
new_x = reshape(last_image,M*N2,1);
xtrain(:,n-2) = new_x;
%figure(2),plot(new_x,'.');
a = strfind(filename,'an2i');
if a
y(n-2) = .9;
else
y(n-2) = .1;
end
end
[A B] = size(y);
randData = randperm(B);
yRand = y(:,randData);
xRand = xtrain(:,randData);
% % Test data
% % Test File
% chosen_folder = 'U:\private\Machine Learning\Neural Networks\ImageReader\All_Faces';
% filename_list = dir(chosen_folder); %this allows us to read all filenames in a folder and save it in a structure
%
% realY = [];
% xtest = [];
% image_datatest = [];
% N = length(filename_list); %the length of the filename structure
% for n = 3:N
% filename = fullfile(chosen_folder,filename_list(n,1).name); %choosing the name of the last file
%
% last_image = imread(filename,'pgm'); %reading the last file
% image_datatest = [image_datatest, last_image];
%
% %figure(1),image(last_image);colormap(gray) %drawing the image,just to understand image files
%
% [M N ] = size(last_image);
% new_x = reshape(last_image,M*N,1);
% xtest = [xtest, new_x];
% %figure(2),plot(new_x,'.');
%
% a = strfind(filename,'an2i');
% if a
% realY(n-2) = 1;
% else
% realY(n-2) = 0;
% end
% end
[EDITED, Jan, Code formatted]

Accepted Answer

Jan
Jan on 17 Oct 2016
The error message means, that the dimensions of image_data1 and last_image do not match. For a horizontal concatenation the number of rows must be equal.
Use the debugger to check this, run this in the command window:
dbstop if error
Now run your code again. Matlab stops, when the error occurs. Then display the sizes of the arrays:
size(image_data1)
size(last_image)
  1 Comment
waqas siddiqui
waqas siddiqui on 17 Oct 2016
Subscripted assignment dimension mismatch.
Error in personIdentifierImageReader (line 25) xtrain(:,n-2) = new_x; even after the dimensions of images are same; the above error is resolved

Sign in to comment.

More Answers (2)

Andreas Krahnke
Andreas Krahnke on 17 Oct 2016
The problem are inconsistent sizes of the matrices you are training to put together. First, you initialize
image_data1 = [];
to be a 0x0 matrix. Then you try to combine it with the newly imported image, which has most likely a different number of rows
image_data1 = [image_data1, last_image];
Since horzcat is the function behind [a,b], the error message means that you cannot put a matrix with 0 rows in front of a matrix with a non-zero number of rows.
If all images have the same number of rows, you should load the first image before the loop to initialize image_data1. Alternatively, you could load the images into a 3D array. Since you seem to know the number of images ahead of time, you could preallocate the array before the loop to save time.

Tobias Kumie
Tobias Kumie on 30 Mar 2020
Dear All,
I have a column matric A of 35040 x 6 and B of 17514 x 6
However when I run it, matlab gives me an error as quoted hereunder;
"Error using horzcat
Dimensions of arrays being concatenated are not consistent".
How can I make both matrix consistent to perform arithmetic operations in matlab?
Thanks
Tobias

Community Treasure Hunt

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

Start Hunting!