我要計算的值的累加值中的dat.txt
低於那些每串2列第1列的期望的輸出被示爲dat2.txt
:如何在MATLAB中執行這個累加和?
dat.txt dat2.txt
1 20 1 20 20 % 20 + 0
1 22 1 22 42 % 20 + 22
1 20 1 20 62 % 42 + 20
0 11 0 11 11
0 12 0 12 12
1 99 1 99 99 % 99 + 0
1 20 1 20 119 % 20 + 99
1 50 1 50 169 % 50 + 119
這是我的初始的嘗試:
fid=fopen('dat.txt');
A =textscan(fid,'%f%f');
in =cell2mat(A);
fclose(fid);
i = find(in(2:end,1) == 1 & in(1:end-1,1)==1)+1;
out = in;
cumulative =in;
cumulative(i,2)=cumulative (i-1,2)+ cumulative(i,2);
fid = fopen('dat2.txt','wt');
format short g;
fprintf(fid,'%g\t%g\t%g\n',[out cumulative(:)]');
fclose(fid);
非常好的主意! +1 – yuk 2010-06-23 18:49:20
我不知道函數DIFF是否僅適用於測試一個條件?如果我想找到滿足兩個條件的累積和,那麼如何在不使用函數FIND的情況下做到這一點? – Jessy 2010-07-08 07:34:32