How to convert a column into equal columns
Afficher commentaires plus anciens
I have a matrix of say:
Number Measurement
1 0.2
2 0.5
3 0.1
1 0.3
2 0.2
3 0.5
1 0.1
2 .07
3 0.9
I want to split this matrix into 3, as in the experiment has been carried out 3 times with 3 measurements each so I can run calculations on each experiment (I actually have a lot more than 3 experiments but they are all of equal number of measurements). I don't need to actually keep the data, I can overwrite the matrix with the one below if I need to as I can save the calculation in another vector. I also don't mind if this is each number is lined up against each other so like this:
1 0.2 1..
2 0.5 2..
3 0.1 3..
etc.
I'be been trying a for i=1:size loop with a nested while number < 4 loop but I hit a problem as the while loop needs to keep running.
Any ideas?
1 commentaire
Azzi Abdelmalek
le 25 Juin 2014
This is not clear
Réponses (1)
>> reshape(reshape(sortrows(d,1),3,[]).',3,[])
ans =
1.0000 0.2000 1.0000 0.3000 1.0000 0.1000
2.0000 0.5000 2.0000 0.2000 2.0000 0.0700
3.0000 0.1000 3.0000 0.5000 3.0000 0.9000
>>
Alternatively, reading the verbiage again, maybe all you're looking for is to get the sets of three one at a time in, say, a loop...
NG=3; % number per group--use variable to change easily
NE=length(x)/NG % number experiments in the dataset
for i=1:NE
i1=(i-1)*NG; % first row in the group location
i2=i*NG; % last...
xy=x(i1:i2,:); % and access that group...
% do whatever with xy here...
...
end
An alternative storage mechanism would be to use a cell array of size NE where each entry is a 2D array holding the data. Then you could loop over it or use cellfun or similar.
Another choice would be a structure array with dynamically named fields of, say 'exp1', 'exp2', etc., for each experiment.
See the documentation on various alternative data structures for more details.
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!