Split a vector into multiple columns by another vector.

조회 수: 1 (최근 30일)
Leif
Leif 2013년 6월 1일
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
  댓글 수: 2
the cyclist
the cyclist 2013년 6월 1일
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
Image Analyst 2013년 6월 1일
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 first vector 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.

댓글을 달려면 로그인하십시오.

답변 (2개)

the cyclist
the cyclist 2013년 6월 1일
편집: the cyclist 2013년 6월 1일
If you are guaranteed the same number of categorical variable, then
[~,idx] = sort(categoricalVariable);
measurementVariable = measurementVariable(idx);
measurementVariable = reshape(measurementVariable,[],5);
  댓글 수: 2
Leif
Leif 2013년 6월 1일
Unfortunately no I do not have the same number of categorical variables. The numbers are the results of a particle tracking model so the variability in the number of values is very high.
Image Analyst
Image Analyst 2013년 6월 1일
편집: Image Analyst 2013년 6월 1일
What are the categories? Do you have multiple types of particles, so you'd have something category #1 = "particle of type #1", and category #2 = "particle of type #2", and so on? Hopefully you're using standard terminology as outlined in http://en.wikipedia.org/wiki/Categorical_variable, because I don't really see why the number of categories should change, even if the number of particles changes. A category could always have a count of zero if no particles of that category existed in that movie frame. Posting an image or figure or diagram would probably help me to understand.

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2013년 6월 1일
mv = randi(120,150,1); %measurement values
cv = randi([1 5],150,1);%categorical identification variable
out = accumarray(cv,mv,[],@(x){x});

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by