Combine vectors of different length into a matrix and perform ANOVA.

Hello,
I have 20 vertical vectors all of different lenghts.
a = 1x876;
b = 1x900;
c = 1x852;
...
y = 1x798;
I'd like to merge them into a single matrix in which each column is one vector (n x 20) and then perform an ANOVA.
Unfortunately a cannot combine vectors of different lenght in one matrix. Is there a way to fill in the empty spaces with a NaN value (probably taking the lenght of the longest one)?
Or maybe there are other methods of which I have no knowledge to perform an ANOVA listing all the variable to be taken into account?
Thank you!

 Accepted Answer

The dimensions you provided indicate that the vectors are row vectors, not column vectors.
If you're performing a 1-ways ANOVA using anova1, you don't have to create a matrix. Use the grouping variable instead. For row vectors,
y = [a,b,c,d];
group = repelem(1:4, 1, [numel(a),numel(b),numel(c),numel(d)])
To answer your original question, pad arrays using B = padarray(A,padsize).

4 Comments

Unfortunately I get this error:
.Error using repelem
In repelem(A, N1, N2, ..., Nn), Nj must be a scalar or a vector of length size(A, j).
I did exactly what you suggested me.
I took all my vectors and stored into y
y = [a,b,c,d];
and then used repelem ;
The first argument is from 1 till the number of vectors (in my case was 1:20), each vector repeated once (1), and then each vector inside numel().
group = repelem(1:4, 1, [numel(a),numel(b),numel(c),numel(d)]);
But I get that error.
update.
I tried to run it using just two of my vectors
>> y = [a, b];
>> group = repelem(1:2, 1, [numel(a), numel(a)]);
and it runs with no errors. So, I'll try to check my syntax, maybe I mispelled something.
CHECKED.
There was a silly syntax mistake. Thank you for your help!
The error is because you forgot the square brackets that group the second input.
repelem(A, N1, N2, ..., Nn)
should probably be
repelem(A, [N1, N2, ..., Nn])
assuming row vector inputs.
Sounds like you figured it out. Glad I could help.

Sign in to comment.

More Answers (0)

Asked:

on 18 Jan 2021

Commented:

on 18 Jan 2021

Community Treasure Hunt

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

Start Hunting!