0
votes

I have a matrix in Matlab where each row looks something like this:

1 3 0.112 5.31275 4.61924 -6.50652

And I want to extract to different matrices according to the value presented in the first column. I know how to do this with for loops but I have too many points to leave this running for whatever time it will take to analyze everything. Is there an easier way to do this?

Here is my code with for loops:

accelerometer = 1;
gyroscope = 0;
a = 1;
g = 1;

for i = 1:size(raw,1)
    if raw(i,1) == accelerometer
        accelData(a,:) = raw(i,2:6);
        a = a+1;
    else
        if raw(i,1) == gyroscope
            gyroData(g,:) = raw(i,2:6);
            g = g+1;
        end
    end
end

Thanks!

1

1 Answers

1
votes

I belive something like the code below could be used:

%# Create dummy matrix
raw = [rand(10,1)>=0.5 rand(10,5)];

accelerometer = 1;
gyroscope = 0;

accelData = raw(raw(:,1)==accelerometer,:)
gyroData = raw(raw(:,1)==gyroscope,:)

Example output:

accelData =

1.0000    0.3517    0.0759    0.1622    0.4505    0.1067
1.0000    0.5853    0.5308    0.3112    0.2290    0.0046
1.0000    0.9172    0.9340    0.1656    0.1524    0.8173
1.0000    0.3804    0.0119    0.6892    0.0782    0.2599

gyroData =

     0    0.8308    0.0540    0.7943    0.0838    0.9619
     0    0.5497    0.7792    0.5285    0.9133    0.7749
     0    0.2858    0.1299    0.6020    0.8258    0.8687
     0    0.7572    0.5688    0.2630    0.5383    0.0844
     0    0.7537    0.4694    0.6541    0.9961    0.3998
     0    0.5678    0.3371    0.7482    0.4427    0.8001