mck
mck

Reputation: 2190

How to apply a function to each row of a matrix and get back a larger matrix

I have a function that accepts a matrix of dimension [1,2] and returns a matrix of dimensions [1,136]. I also have a matrix of dimensions [N,2]. I want to apply this function to each row of the matrix to finally get a matrix of dimensions [N,136].

I am completely lost on how to do this in Matlab. A for loop solution would be enough (I can't even do that at this point), but as far as I know in Matlab there are better and more parallelizable ways of doing things.

My current attempt looks like this:

  phi = arrayfun(@(x,y) gaussianBasis([x y])' , trainIn(:,1), trainIn(:,2), 'UniformOutput', false);

where gaussianBasis is a function returning a vector [136,1] and trainIn is a matrix [N,2]. phi is supposed to be [N,136], but this returns an array of N cell arrays each containing a matrix [1,136].

Thanks for all the help!

Upvotes: 2

Views: 310

Answers (1)

abcd
abcd

Reputation: 42245

You just need to use cat and concatenate it along the first dimension:

phi = cat(1, phi{:})

This should give you an N x 136 matrix

Upvotes: 4

Related Questions