As I can compare rows in a column?

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

doesn't work... I'm not very good with programming, returns this: warning: range error for conversion to character value warning: implicit conversion from int32 matrix to sq_string error: prueba2: A(I,J): column index out of bounds; value 1 out of bound 0 error: at line 8, column 15
Hi Marta! Please attach file with your data.
Andrei , the page will not let me load that file type, and if I export to pdf the last column is passed to a new row, if you want to upload the pdf, but knowing that the last column is passed to a new row, this is how it should appear
Andrei Bobrov
Andrei Bobrov on 24 Jul 2015
Edited: Andrei Bobrov on 24 Jul 2015
Please see 'add variant'. It for data from attach file 'your_data_2.txt'.
works!! Now if I just want to take the 6,10 and 11 column instead of all, I change the "d" function this way : d = textscan(f,['%*d %*s %*d %*d %*d %d %*d %*d %*d %d',repmat(' %f',1,9) ],'collectoutput',1);
Andrei Bobrov
Andrei Bobrov on 24 Jul 2015
Edited: Andrei Bobrov on 24 Jul 2015
small correcting (see my answer).
OR You can rename your file as 'prueba.224.txt' and use expression:
f = fopen('full_path_for_folder_with_your_file\prueba.224.txt');
no works like i want, it still prints all columns, and I just want 3 of them :( I've tried here:
if true
f = fopen('prueba.224');
d = textscan(f,['%d %s',repmat(' %f',1,9) ],'collectoutput',1);
fclose(f);
[~,~,c] = unique(d{3}(:,2));
columnas = accumarray(c,(1:numel(c)).',[],@(ii){Mc(ii,:)});
diff(c);
A=find(diff(c));
end
so that search index who are non-zero and returns the previous me. But now it does not know how to relate this to the column 6 and column 10
Please try it:
f = fopen('prueba.224');
d = textscan(f,['%d %s',repmat(' %f',1,9) ],'collectoutput',1);
fclose(f);
[~,~,c] = unique(d{3}(:,2));
ii = accumarray(c,(1:numel(c)).',[],@(ii)ii(1));
out = d{3}(ii,[4,8]);
if i add a 9 in out;
out = d {3} (ii, [4,8,9]);
Prints the 3 columns that I need to use but i don't see how that can take me to what I asked that the last column in this case 9 is separated into different groups of rows when they are different data ... I'm trying to do for here but not that failure has the code:
f = fopen('prueba.224');
d = textscan(f,'%*d %*s %*d %*d %*d %d %*d %*d %*d %d %f ');
fclose(f);
elv=d{1};
dif=d{2};
t=d{3};
i=1;
j=1;
ii=length(t);
while i<ii
Eq(i)=find(t==t(i));
L=length(Eq);
pesos(j)=(sin(elv(Eq(i))))^2;
x1(j)=sum(dif(Eq(i))*pesos(j))/sum(pesos)
i=i+L;
i=i+L;
j=j+1;
end
this returns me fails, but i don't know how can i fix
From what I can see, your way I returned only 85 data and not the 521 I guess you already have clustered. but it makes me only the first data of each "group" which coincide, but I need to see the whole group to apply a formula to the column 6 and 10 (in our case 4 and 8 out)
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
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)

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

Categories

Asked:

on 23 Jul 2015

Edited:

on 30 Jul 2015

Community Treasure Hunt

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

Start Hunting!