How could I use a loop in this case?

조회 수: 1 (최근 30일)
Sandy
Sandy 2016년 6월 13일
댓글: Stephen23 2016년 6월 13일
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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 13일
편집: Azzi Abdelmalek 2016년 6월 13일
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]
  댓글 수: 1
Sandy
Sandy 2016년 6월 13일
Yes! Thank you so much!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by