user999450
user999450

Reputation: 249

How to add a matrix( not the same dimension) below another matrix

I know how to add a matrix below another matrix.

e.g A = [A; B];

I have two matrixes of different dimensions(both different number of columns and rows) and i need to add them up together. One matrix is the main one and the other one is being added to it. Both of them are in a for loop and so the dimension of the first matrix is keep changing.

e.g for i = 1 :3
      AngerHist = [AngerHist;Hist];
    end

When I run it is giving me this error :

??? Error using ==> vertcat CAT arguments dimensions are not consistent. – 

How do i do it? Any suggestions?

AngerHist = {}; DisgustHist = {};FearHist = {}; HappyHist = {}; SadHist = {};SurpriseHist = {};
Images = 'C:\Users\HP\Documents\MATLAB\Images';
List = dir(Images);
n= size(List,1);

for i = 3:n
    List1 = dir(fullfile(Images,List(i).name));
    m= size(List1,1);
    BASE_DIR = fullfile(Images,List(i).name);
    for j = 3: m
        I=List1(j).name;
        I1= imread(fullfile(BASE_DIR,I),'jpg');
        Name = List(i).name;
        switch (Name)
                 case 'Anger'
                     Hist = UniformLBP(I1);
                     AngerHist = {AngerHist;Hist};
                     break;
                 case 'Disgust'
                     Hist = UniformLBP(I1);
                     DisgustHist = {DisgustHist;Hist};
                     break;
                case 'Fear'
                    Hist = UniformLBP(I1);
                    FearHist = {FearHist;Hist};
                    break;
                case 'Happy'
                    Hist = UniformLBP(I1);
                    HappyHist = {HappyHist;Hist};
                    break;
                case 'Sad'
                    Hist = UniformLBP(I1);
                    SadHist = {SadHist;Hist};
                    break;
                case 'Surprise'
                    Hist = UniformLBP(I1);
                    SurpriseHist = {SurpriseHist;Hist};
                    break;
        end        

    end    
end

Upvotes: 1

Views: 3399

Answers (3)

Oli
Oli

Reputation: 16035

I think that you need to use cells:

A={};
A=[A;rand(2,3)];
A=[A;rand(3,4)];
A=[A;rand(4,5)];
A=[A;rand(6,5)];

The result is:

A = 
 [2x3 double]
 [3x4 double]
 [4x5 double]
 [6x5 double]

Upvotes: 2

kazimpal
kazimpal

Reputation: 300

The problem is not that the matrix size is changing inside the loop. That is allowed. The problem is that to vertically stack two matrices, they each need to have the same number of columns.

Imagine having two matrices:

A = [ a b c ;
      d e f ;             
      g h i ]

B = [ q r;
      s t ]

and you try to perform:

C = [A;B]

what dimensions would C have? It's undefined.

You could pad B with a column of zeros (or something) to make it the right width so C would end up looking like:

| a b c |
| d e f |
| g h i |
| q r 0 |
| s t 0 |

or

| a b c |
| d e f |
| g h i |
| 0 q r |
| 0 s t |

but that is just a hack to make the matrix stacking operation work.

If the matrices are always different widths you should think about what you're actually trying to do with them and perhaps consider storing them inside a cell array.

Upvotes: 2

mtrw
mtrw

Reputation: 35088

To vertically stack two matrices, they must have the same number of columns. For instance:

A = rand(3,2);
for ctr = 1:3
    B = rand(4,2);
    A = [A ; B];
end

At the end, A will be 15x2. Similarly, if the matrices have the same number of rows, you can horizontally stack them.

If the matrices have different numbers of rows and columns, then it does not make sense to use a matrix to represent their collection. You might consider making each matrix an entry in a cell array instead. See the documentation for more information.

Upvotes: 2

Related Questions