Copying rows from a matrix that meet a condition into new matrices

12 visualizzazioni (ultimi 30 giorni)
I know this is a rookie question, but I am a rookie MATLAB user...
I have the following matrix:
32 734786.452997685 5.70000000000000 361
32 734786.452997685 5.70000000000000 367
1 734786.452997685 5.69500000000000 100
31 734786.453009259 5.69000000000000 175
31 734786.453009259 5.69000000000000 182
what i need to do is put the rows into different matrices according to the value in the first column. so one matrix that only has the '1' rows, one that only has the '31' rows, and one that has only the '32' rows.
this is bloomberg bid/ask/trade data. if anyone knows how to write the bloomberg function that returns the individual bid, ask, and trade data elements i'd gladly take that also.
suggestions?

Risposta accettata

Walter Roberson
Walter Roberson il 4 Nov 2011
Ufirst = unique(YourData(:,1));
nval = length(Ufirst);
SeparatedData = cell(nval,1);
for K = 1:nval
SeparatedData{K} = YourData(YourData(:,1)==Ufirst(K),:);
end
Then SeparatedData{1}, SeparatedData{2} etc would be the matrices.
You might also wish to consider instead
sortrows(YourData)
as you might find that it is not necessary to extract the data into distinct matrices if it is grouped together.
In the longer term, depending what you are doing with the data, you may wish to use John D'Errico's File Exchange Contribution considator()

Più risposte (2)

Fangjun Jiang
Fangjun Jiang il 4 Nov 2011
Use logical index.
d=[32 734786.452997685 5.70000000000000 361
32 734786.452997685 5.70000000000000 367
1 734786.452997685 5.69500000000000 100
31 734786.453009259 5.69000000000000 175
31 734786.453009259 5.69000000000000 182];
UniCol=unique(d(:,1));
N=numel(UniCol);
Out=cell(N,1);
for k=1:N
index=d(:,1)==UniCol(k);
Out{k}=d(index,:);
end

Timothy Stark
Timothy Stark il 4 Nov 2011
Thank you both for your help. I'm at least glad it wasn't a one line answer....

Categorie

Scopri di più su Financial Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by