Asked by Leif
on 1 Jun 2013

I have two large vectors. The first is a list of measurement values and the second is a categorical identification variable (5 different variables). I am trying to create a matrix of 5 columns (one for each categorical identifier) with all of the measurement values as the different rows?

Thanks

*No products are associated with this question.*

Answer by the cyclist
on 1 Jun 2013

Edited by the cyclist
on 1 Jun 2013

If you are guaranteed the same number of categorical variable, then

[~,idx] = sort(categoricalVariable); measurementVariable = measurementVariable(idx); measurementVariable = reshape(measurementVariable,[],5);

Image Analyst
on 1 Jun 2013

Answer by Andrei Bobrov
on 1 Jun 2013

mv = randi(120,150,1); %measurement values cv = randi([1 5],150,1);%categorical identification variable

out = accumarray(cv,mv,[],@(x){x});

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 2 Comments

## the cyclist (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/77656#comment_152299

Are you guaranteed that the categorical identification variables appear the same number of times, so that your columns have equal length? Or might you need to pad them?

## Image Analyst (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/77656#comment_152300

How is the second vector "large" when it has only 5 categorical identifications, like {'Class1', 'Class2', 'Class3', 'Class4', 'Class5'}? Or is the second vector really a N by 5 array of numbers??? How large is "large"? 50 million? 300 million? If you're talking about something less than a few million, that's not large. If only the

firstvector is measurements, how are you going to get 5 more columns of measurements for the output array? Where are those extra measurements going to come from or how are they to be calculated? I think you'd best give a small example, maybe with 5 or 10 rows or something.