As I can compare rows in a column?

1 view (last 30 days)
I have a file that has 11 columns, these columns I'm interested 3 (actually 2, the third helps me to compare rows) I apply:
fid=fopen("prueba.224");
C =textscan(fid,'%*d %*s %*d %*d %*d %d %*d %*d %*d %d %f ');
fclose(fid);
celldisp(C);
And it goes well, but I need the third column that prints compare the ranks. So divided into groups the other two columns where the row of the third column are equal.
File example:
</matlabcentral/answers/uploaded_files/34548/Captura%20de%20pantalla%202015-07-23%20a%20las%2014.57.24.png> my goal is, from the last column split the data into groups of rows where these coincide in the last column, and then these groups want to get two columns of concrete that if I can do as I put in command previous

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 23 Jul 2015
Edited: Andrei Bobrov on 24 Jul 2015
f = fopen('your_data.txt');
c = textscan(f,'%s','delimiter','\n');
fclose(f);
c = c{:}
z = regexp(c,'-?\d+(\.\d+)?|\w+','match');
x = cat(1,z{:});
x(:,[1,3:end]) = cellfun(@str2double,x(:,[1,3:end]),'un',0);
t = cell2mat(x(:,4));
[~,~,c1] = unique(t);
out = accumarray(c1,(1:numel(t))',[],@(ii){x(ii,:)});
add variant
f = fopen('full_path_for_folder_with_your_file\prueba.224');
d = textscan(f,['%d %s',repmat(' %f',1,9) ],'collectoutput',1);
fclose(f);
Mc = [num2cell(d{1}),cat(1,d{2}),num2cell(d{3})];
[~,~,c] = unique(d{3}(:,2));
out = accumarray(c,(1:numel(c)).',[],@(ii){Mc(ii,:)});
  11 Comments
marta gc
marta gc on 30 Jul 2015
DONE!!!
f = fopen('prueba.224');
d = textscan(f,'%*d %*s %*d %*d %*d %f %*d %*d %*d %f %s ');
fclose(f);
nume = ((sin(d{1}(1)))^2)*(d{2}(1));
den = ((sin(d{1}(1)))^2);
long = length(d{1});
i=1;
j=1;
while (i < long)
if ( strcmp(d{3}(i),d{3}(i+1)) )
i = i + 1;
nume = nume + ((sin(d{1}(i)))^2)*(d{2}(i));
den = den + ((sin(d{1}(i)))^2);
else
A(j,1) = nume/den;
i = i + 1;
j = j + 1;
nume = ((sin(d{1}(i)))^2)*(d{2}(i));
den = ((sin(d{1}(i)))^2);
end
end
Andrei Bobrov
Andrei Bobrov on 30 Jul 2015
f = fopen('prueba.224.txt');
d = textscan(f,'%*d %*s %*d %d %*d %f %*d %*d %*d %f %*f');
fclose(f);
[~,~,c] = unique(d{1},'stable');
d21 = sin(d{2}).^2;
d22 = d21.*d{3};
A0 = accumarray([[c;c],kron([1;2],ones(numel(c),1))],[d22;d21]);
A = A0(:,1)./A0(:,2);

Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 30 Jul 2015
The followup discussion in this thread is not exactly clear, but if the goal (as stated in the original post) is to "divided into groups the other two columns where the row of the third column are equal", then this does that:
>> t = readtable('prueba.224.txt','Format','%d%s%d%d%d%d%d%d%d%d%f','delimiter',' ','MultipleDelimsAsOne',true,'ReadVariableNames',false);
>> tGrouped = varfun(@(x){x},t,'GroupingVariable','Var11','InputVariables',{'Var6' 'Var10'})
tGrouped =
Var11 GroupCount Fun_Var6 Fun_Var10
____________ __________ ___________ ___________
57224.001389 57224.001389 5 [5x1 int32] [5x1 int32]
57224.0125 57224.0125 5 [5x1 int32] [5x1 int32]
57224.023611 57224.023611 7 [7x1 int32] [7x1 int32]
57224.034722 57224.034722 7 [7x1 int32] [7x1 int32]
57224.045833 57224.045833 6 [6x1 int32] [6x1 int32]
57224.056944 57224.056944 6 [6x1 int32] [6x1 int32]
...
In other words, there are 5 rows where the 11th column is equal to 57224.001389, and these are the values of the 6th and 10th columns in those rows:
tGrouped.Fun_Var6{1}
ans =
340
726
456
660
308
tGrouped.Fun_Var10{1}
ans =
61
51
57
52
48

Community Treasure Hunt

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

Start Hunting!