Error using ==> horzcat CAT arguments dimensions are not consistent.

2 views (last 30 days)
I want to find feature vectors.. for that i executed the following code
clear all; close all; clc;
im1=imread('lena.jpg');
im1g=rgb2gray(im1);
%%Calculating mean of im1
grayImage=rgb2gray(im1);
subplot(1, 2, 1);
imshow(grayImage, []); title('Original Grayscale Image');
% Let's get its histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(1, 2, 2); bar(pixelCount); title('Histogram of original image');
xlim([0 grayLevels(end)]); % Scale x axis manually.
yRange = ylim; % Calculate the mean gray level
meanGL = sum(pixelCount .* grayLevels) / sum(pixelCount);
%%Calculating standard deviation
st_d1=std(double(im1));
%%Calculating Skewness
sk1=skewness(double(im1));
%%Calculating RMS
im1=double(im1);
rms1=rms(im1);
%%Calculating median absolute
md1=mad(double(im1));
%%Contruct a feature vector
size(meanGL2)
size(st_d2)
size(sk2)
size(rms2)
size(md2)
fv1=[ meanGL, st_d1, sk1, rms1, md1];
and i get that concatenation error .. the size of the variables displayed in the output screen are
ans =
1 1
ans =
1 512 3
ans =
1 512 3
ans =
1 512 3
ans =
1 512 3
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Error in ==> fvector at 64
fv1=[ meanGL, st_d1, sk1, rms1, md1];
i am new to matlab and so dont know how to fix it.. if possible any solutions..

Accepted Answer

dpb
dpb on 30 Jan 2014
Since you say you're trying to construct a feature vector from the expression
fv1=[ meanGL, st_d1, sk1, rms1, md1];
but of the five only the first,
meanGL = sum(pixelCount .* grayLevels) / sum(pixelCount);
isn't a higher dimension than single, I presume what you intended to write for
%%Calculating standard deviation
st_d1=std(double(im1));
and the remaining would have been
st_d1=std(im1(:));
which will return the std dev of the entire image array im1 whereas the default behavior of std in Matlab is to operate on the planes hence return the 1x512x3 size. See
doc std
for further description/details. The (:) notation casts the array into a vector so the operation is then done on that one column and the result is then the desired single value for the entire image.
Same logic/fix for the rest.
  2 Comments
dpb
dpb on 30 Jan 2014
BTW, if there's no other need for the computed values than the building of the vector, then you may as well dispense with the intermediaries entirely--
fv1=[meanGL std(im1) skewness(im1) rms(im1) mad(im1)];
Note I left the meanGL out simply because it is a computation and the abstraction suits my eye to reduce the complexity in the final result but there's no reason beyond that for doing so; it could just as well be computed in situ as well as long as the result isn't needed by itself elsewhere.

Sign in to comment.

More Answers (0)

Categories

Find more on Images 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!