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.

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

Thanks! I don't completely understand it, but I'll run your program to try to figure out what it is actually doing. Could you add some comments?
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
I can create the cell arrays, like you said. But it doesn't solve the problem, since I can't operate on the data. For example, I created the cell array, and it displays as >> disp(C89) Columns 1 through 5
[] [] [6x4 double] [6x4 double] [6x4 double]
Columns 6 through 9
[6x4 double] [8x4 double] [8x4 double] [8x4 double]
Columns 10 through 12
[8x4 double] [8x4 double] [8x4 double]
for i = 3:12. Making an array from C89{3} through C89{6} is ok, but the same command doesn't work when matlab encounters [8x4 double]. So having cell arrays with different sized arrays doesn't seem to help matters.
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.
Ok, I did some 'Help' searching on cellarrays, and found out how to get the numbers out. Now I'm struggling with writing the calculations to another array in a meaningful way.
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.
Here it is. I always seem to have problems assigning values to an array without them getting written over:
intval89 = zeros(10,3); % desired output
for i = 3:12 % identifier for sequence in C89
N89 = C89{:,i}; %produces an 8 row x 4 col array
intchl89 = trapz(N89(:,2),N89(:,3));
intpp89 = trapz(N89(:,2),N89(:,4));
for j=1:10
% below is wrong but need to fill up the array with these values
intval89(j) = [i intchl89 intpp89];
break
end
end
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
Well that worked! Thanks for all the help. Now I can have a weekend!
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

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.

This question is closed.

Tags

Asked:

on 16 Apr 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!