How to pull out and sort data from 48772x15 matrix

1 visualización (últimos 30 días)
Matthew
Matthew el 20 de Abr. de 2015
Comentada: Stephen23 el 20 de Abr. de 2015
Hello,
I have a 48772x15 matrix with data in columns 1-14. In column 15 there are 0s with the occasional nonzero number (from 1-8) as a marker for a project I'm doing. What I need help with is when there is a nonzero number I want to take/grab the data from all columns from that row until there is another nonzero number in that column and put that data into a different area so I can sort it. ie. when marker number 6 shows up I want to take that data until there is another nonzero marker in that column and then put that data with the other number 6's data.
I am not that proficient in matlab so any help would be beneficial. I know I should use an else if statement and grab data but not sure how to formulate it.
Thank you.
  3 comentarios
Matthew
Matthew el 20 de Abr. de 2015
The markers are the start of the new block! The markers at the end of the old block are ignored since they are the start of the next one. Sorry for not clarifying
Stephen23
Stephen23 el 20 de Abr. de 2015
Do you want to concatenate the groups of arrays, or keep them as individual? Or, to put it in the context of your example, do you want a the 6's to be all joined together, or "set" of 6's?

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 20 de Abr. de 2015
Editada: Stephen23 el 20 de Abr. de 2015
Option One: Do not Separate the Data
MATLAB works fastest on simple numeric matrices and has many inbuilt functions that you can use on simple matrices. When you split up your data and place it in some kind of complicated data structure (e.g. cell array or structure) then it actually just makes the code slower and more complicated: you should only do this if you really have to. It might be possible to simply keep the data all together in one numeric array and use some simple indexing like this whenever you need to refer to the data:
>> A(:,1) = 1:10; % one column of fake data
>> A([1,3,6,8],2) = [3,2,1,2] % markers at start of each block
A =
1 3
2 0
3 2
4 0
5 0
6 1
7 0
8 2
9 0
10 0
Firstly we "fill-in" the missing markers:
>> B = find(A(:,2));
>> C = diff([B;1+size(A,1)]);
>> D = arrayfun(@(a,r)repmat(a,r,1), A(B,2), C, 'UniformOutput',false);
>> A(:,2) = vertcat(D{:})
A =
1 3
2 3
3 2
4 2
5 2
6 1
7 1
8 2
9 2
10 2
Which lets us refer to any group all at once, e.g. here we can calculate the mean of the 2 category, without any loops ( vectorized ), even though this category is non-consecutive:
>> X = A(:,2)==2;
>> mean(A(X,1))
ans =
6.5000
Option Two: use mat2cell to separate the Data
>> E = mat2cell(A,C,2);
then comes the question of how you want to collect the different categories together: concatenate them, or keep them as separate arrays, or something else... you need to decide this.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by