while loop question number 2
信息
此问题已关闭。 请重新打开它进行编辑或回答。
显示 更早的评论
My first question was answered, but wasn't a complete solution. Here's a portion of the data file:
79, 970108, 1, 5, 0.088, 6.383,
79, 970108, 1, 25, 0.075, 4.380,
79, 970108, 1, 45, 0.094, 0.710,
79, 970108, 1, 75, 0.221, 3.010,
79, 970108, 1, 100, 0.140, 0.793,
79, 970108, 1, 125, 0.094, 0.243,
79, 970108, 1, 150, 0.054, 0.117,
79, 970108, 1, 175, 0.022, 0.043,
80, 970218, 1, 5, 0.097, 6.293,
80, 970218, 1, 25, 0.096, 4.920,
80, 970218, 1, 45, 0.094, 2.533,
80, 970218, 1, 75, 0.125, 3.520,
80, 970218, 1, 100, 0.198, 2.257,
80, 970218, 1, 125, 0.222, 0.723,
80, 970218, 1, 175, 0.072, 0.107,
81, 970312, 1, 5, 0.091, 6.983,
81, 970312, 1, 25, 0.084, 1.547,
81, 970312, 1, 45, 0.098, 4.853,
81, 970312, 1, 75, 0.173, 2.877,
81, 970312, 1, 125, 0.127, 0.550,
81, 970312, 1, 150, 0.054, 0.120,
82, 970409, 1, 5, 0.064, 2.905,
etc
the idea is to integrate on column 4, but the only identifier for the range of values over which to integrate is the value in column 1. I can't seem to write a routine that says "while column 1 == some value, store columns 4, 5, and 6 in vectors, then integrate 5 and 6 over column 4, and then store the integrate values in an array. Then go onto the next column 1 identifier." Any help for this programming newbie would be appreciated. In the above, 79 is associated with 8 values to integrate, 80 with 7, and 81 with 6.
0 个评论
回答(2 个)
Iman Ansari
2013-4-16
编辑:Iman Ansari
2013-4-16
Hi
A=[ 79, 970108, 1, 5, 0.088, 6.383
79, 970108, 1, 25, 0.075, 4.380
79, 970108, 1, 45, 0.094, 0.710
79, 970108, 1, 75, 0.221, 3.010
79, 970108, 1, 100, 0.140, 0.793
79, 970108, 1, 125, 0.094, 0.243
79, 970108, 1, 150, 0.054, 0.117
79, 970108, 1, 175, 0.022, 0.043
80, 970218, 1, 5, 0.097, 6.293
80, 970218, 1, 25, 0.096, 4.920
80, 970218, 1, 45, 0.094, 2.533
80, 970218, 1, 75, 0.125, 3.520
80, 970218, 1, 100, 0.198, 2.257
80, 970218, 1, 125, 0.222, 0.723
80, 970218, 1, 175, 0.072, 0.107
81, 970312, 1, 5, 0.091, 6.983
81, 970312, 1, 25, 0.084, 1.547
81, 970312, 1, 45, 0.098, 4.853
81, 970312, 1, 75, 0.173, 2.877
81, 970312, 1, 125, 0.127, 0.550
81, 970312, 1, 150, 0.054, 0.120
82, 970409, 1, 5, 0.064, 2.905];
C={};
for i=79:81
C{i}=A(A(:,1)==i,logical([0 0 0 1 1 1]));
end
disp('C{79}=')
disp(C{79})
disp('C{80}=')
disp(C{80})
disp('C{81}=')
disp(C{81})
9 个评论
John
2013-4-16
Iman Ansari
2013-4-16
C is a cell array, Rows that have same value in column one can be indexing A(A(:,1)==79). This return first 8 rows, for only having column 4,5 and 6 this became A(A(:,1)==79,4:6):
C={};
for i=79:81
C{i}=A(A(:,1)==i,4:6);
end
John
2013-4-19
Iman Ansari
2013-4-19
What you want to do with these matrices? You can write your commands and use size(C89{i},1) for number of rows in each matrix.
John
2013-4-19
Iman Ansari
2013-4-19
编辑:Iman Ansari
2013-4-19
maybe:
M=C89{3};
Sum_of_Each_Columns89(3,:)=sum(M,1);
Post your code or give me an example to better understand what you want.
John
2013-4-19
Iman Ansari
2013-4-19
intval89 = zeros(10,3); % desired output
for i = 3:12 % identifier for sequence in C89
N89 = C89{:,i}; % Why not use C89{i}
intchl89 = trapz(N89(:,2),N89(:,3));
intpp89 = trapz(N89(:,2),N89(:,4));
%%%%%%%%%%%%%
intval89(i,:) = [i intchl89 intpp89];
end
John
2013-4-19
Ahmed A. Selman
2013-4-16
In such a case, using a while loop cannot do the job, but an if statement can. So, I think this is what are you looking for (continued from the previous question/answer):
clear
clc
% 1) Reading data from a text file. Change the name of the file first.
filename='johnText.txt'; % Change this filename
ID1=fopen(filename,'r');
[A b]=fscanf(ID1,'%g %*c',[1 inf]); % *c is to ignore the comma in the file
B=reshape(A,6,b/6); % 6 elements each line having numeric values w/no comma
B=B';
% 2) The math workout
[a2,b2]=size(B); % Now B=[Num X Y Z]
k=0;Acc=0;
CalCol=4; % Which column is to be used for sum or integration?
RefCol=1; % Which column is to be used as matching reference?
DS(1:a2)=B(1:a2,CalCol);% first one only
for i=1:a2 % on rows
j0=DS(i);
if i==a2;break;end
if ( B(i,RefCol)==B(i+1,RefCol) ) % if Num of B(1,j)=Num of B(1,j+1)
Acc=Acc+B(i,CalCol);
else
k=k+1;
A2(k)=Acc+B(i,CalCol);
Acc=0;
end
end
disp('Your summation of col4 when col.1 is the same is: ');disp(' ');
disp(A2')
Please note that this is a code doing the summation over the 4th column whenever the first one is matched between successive rows. If you find it tricky to add another type of math instead of plane summation, do not hesitate to tell me about it.
Regards.
1 个评论
John
2013-4-17
此问题已关闭。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!