How could I use a loop in this case?

1 view (last 30 days)
This is going to be a little difficult to explain. I have a data set in the following format:
2010 08 09 07 14 40.26 6.2 30.391703 094.836545 0.70 0680912525.25
2012 08 09 05 49 55.93 5.6 30.321556 094.844720 0.81 0680912525.25
2012 08 07 00 17 54.54 5.5 30.350000 094.796998 0.81 0680912525.25
1991 07 30 22 22 05.25 4.6 30.374200 094.806700 1.00 0680912525.25
1997 09 23 20 03 37.69 3.5 36.639100 071.257200 1.00 0875045017.69
2006 03 22 17 56 28.57 3.5 36.597432 071.169553 0.43 0875045017.69
2006 06 23 18 47 18.57 3.5 36.597432 071.169553 0.41 0875045017.69
I'd like to go through the 11th column and group all the ones that are the same (we'll call this a set). I'd then like to subtract all the numbers in the 7th column from the largest number in that set and append them as a new column.For example:
2010 08 09 07 14 40.26 6.2 30.391703 094.836545 0.70 0680912525.25 0
2012 08 09 05 49 55.93 5.6 30.321556 094.844720 0.81 0680912525.25 0.6
2012 08 07 00 17 54.54 5.5 30.350000 094.796998 0.81 0680912525.25 0.7
1991 07 30 22 22 05.25 4.6 30.374200 094.806700 1.00 0680912525.25 1.6
1997 09 23 20 03 37.69 4.5 36.639100 071.257200 1.00 0875045017.69 0
2006 03 22 17 56 28.57 3.5 36.597432 071.169553 0.43 0875045017.69 1
2006 06 23 18 47 18.57 3.5 36.597432 071.169553 0.41 0875045017.69 1
I have the following, but this stops at the first four lines.
y = [];
i = 1;
while (data (i,11) == data ((i+1),11))
y(i + 1) = data(1, 7) - data(i+1, 7)
i = i + 1;
end

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 13 Jun 2016
Edited: Azzi Abdelmalek on 13 Jun 2016
A=[2010 08 09 07 14 40.26 6.2 30.391703 094.836545 0.70 0680912525.25
2012 08 09 05 49 55.93 5.6 30.321556 094.844720 0.81 0680912525.25
2012 08 07 00 17 54.54 5.5 30.350000 094.796998 0.81 0680912525.25
1991 07 30 22 22 05.25 4.6 30.374200 094.806700 1.00 0680912525.25
1997 09 23 20 03 37.69 4.5 36.639100 071.257200 1.00 0875045017.69
2006 03 22 17 56 28.57 3.5 36.597432 071.169553 0.43 0875045017.69
2006 06 23 18 47 18.57 3.5 36.597432 071.169553 0.41 0875045017.69]
[ii,jj,kk]=unique(A(:,11))
c=cell2mat(accumarray(kk,(1:numel(kk))',[],@(x) {-A(x,7)+max(A(x,7))}))
out=[A c]

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 13 Jun 2016
[~,~,c] = unique(A(:,11));
z = accumarray(c,A(:,7),[],@max);
out =[A,z(c) - A(:,7)];

Categories

Find more on Automotive in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!