Info

This question is closed. Reopen it to edit or answer.

while loop question number 2

1 view (last 30 days)
John
John on 16 Apr 2013
Closed: MATLAB Answer Bot on 20 Aug 2021
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.

Answers (2)

Iman Ansari
Iman Ansari on 16 Apr 2013
Edited: Iman Ansari on 16 Apr 2013
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 Comments
Iman Ansari
Iman Ansari on 19 Apr 2013
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
John on 19 Apr 2013
Well that worked! Thanks for all the help. Now I can have a weekend!

Ahmed A. Selman
Ahmed A. Selman on 16 Apr 2013
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 Comment
John
John on 17 Apr 2013
Thanks. Very helpful, I think.
I read in the data file using csvread('textfile.csv'), and instead of doing an accumulation, I just used the function trapz.
I think you're right that the 'if' statement is the way to go instead of 'while'. I'll have to educate myself on some of the other things, like size. Anyway, much appreciated.

Tags

Community Treasure Hunt

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

Start Hunting!