Having each row compare its string with other rows within the problem and getting corresponding data relative to the string in other columns.

Hello,
I am stuck at this point where I want to run a for loop through the column with multiple rows has strings. I want to compare each row to its adjacent row if same then take its corresponding values. Each time the string is different.
I am unable to use strcmp/pattern as I can't list all the strings that need to be compared.
I was hoping if I could run a for loop on the first column and each row is compared to every ROW and same string will extract itself together.
Have attahced a sample file with Dummy variables.
*******************************************************************************************************************************************************************
A = readtable("C:\Users\gudlu\OneDrive\Documents\Matlab\Book1.xlsx");
headers = table2array(A(:,1));
Sorry unable to move beyond this.

5 commentaires

Please show the expected output using your uploaded data file.
I don't understand what you want to do. The phrase "compare each row to its adjacent row if same then take its corresponding values" is unclear to me.
Here is a suggestion for making your question clearer:
  • Post a much smaller version of the input file, with just enough rows to illustrate what you are trying to do.
  • Post the output you expect from that smaller input file. Don't just describe the output you want. Show the output you want.
Sorry about that. thank you for commenting
A = readtable("C:\Users\gudlu\OneDrive\Documents\Matlab\Book1.xlsx");
headers = table2array(A(:,1));
compareStrings = {'ABCDF';'defggt';'ALLLDKKD'};
Headers = length(headers);
for I = 1:Headers
s = strcmp(headers,compareStrings);
NewHeaders = s(I);
end
Below I have added a few snippets.
when I have just one string in the compareStrings Cell 's' populates with logical values but with multiple strings it does not. My 1st column parameters in the first column would be changing so I do not want to change my compare string variable {Cell} each time. So I was hoping to compare the parameters within row aganist each other and then each parameter can be stored as its paramter name and have multiple column array with its respective values. Hope I am clear now.
Desired Output Input
The sample data file you uploaded includes the text ABCDF four times, on rows 1, 41, 45, and 49. You only include rows 1, 45, and 49 in the desired output, i.e. row 41 is excluded. I do not understand the rule, perhaps I missed that part of your explanation: please clarify why row 41 is not part of the output.
I also don't understand how your exected output matches your description "I want to compare each row to its adjacent row if same then take its corresponding values". As far as I can tell, you did not compare adjacent rows, but rather all rows with the text in cell A1 (plus some as-yet secret rule for ignoring other rows). In any case, rows 45 and 49 are not adjacent to A1, so it is unclear to me what you want.
It isn't that is not part of my output I just pasted a small snippet and a quick example. It is part of it. As mentioned earlier if the headers are same they need to be grouped together irrespective of their number of occurances.

Connectez-vous pour commenter.

 Réponse acceptée

Not sure if you want this
A=readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1460327/Book1.xlsx');
header=A.Var1;
compareStrings = {'ABCDF';'defggt';'ALLLDKKD'};
C = cell(size(compareStrings));
for k=1:length(compareStrings)
C{k} = A(strcmp(header, compareStrings{k}),:);
end
C{:}
ans = 4×3 table
Var1 Var2 Var3 _________ ________ ________ {'ABCDF'} 0.52474 0.85068 {'ABCDF'} 0.14364 0.25306 {'ABCDF'} 0.030389 0.068798 {'ABCDF'} 0.6799 NaN
ans = 14×3 table
Var1 Var2 Var3 __________ _______ _______ {'defggt'} 0.80452 0.55857 {'defggt'} 0.12369 0.35813 {'defggt'} 0.6379 0.25596 {'defggt'} 0.89595 0.46676 {'defggt'} 0.54452 0.43122 {'defggt'} 0.76044 0.40233 {'defggt'} 0.38287 0.85625 {'defggt'} 0.73387 0.37358 {'defggt'} 0.83975 0.21899 {'defggt'} 0.82822 0.43342 {'defggt'} 0.12952 0.07045 {'defggt'} 0.87132 0.91706 {'defggt'} 0.7532 0.18353 {'defggt'} 0.55729 NaN
ans = 12×3 table
Var1 Var2 Var3 ____________ ________ _______ {'ALLLDKKD'} 0.81691 0.90177 {'ALLLDKKD'} 0.821 0.48899 {'ALLLDKKD'} 0.01612 0.92917 {'ALLLDKKD'} 0.51538 0.25401 {'ALLLDKKD'} 0.60644 0.70253 {'ALLLDKKD'} 0.85535 0.18184 {'ALLLDKKD'} 0.084649 0.5842 {'ALLLDKKD'} 0.33199 0.22169 {'ALLLDKKD'} 0.37172 0.52223 {'ALLLDKKD'} 0.17652 0.7413 {'ALLLDKKD'} 0.083156 0.75819 {'ALLLDKKD'} 0.70004 0.73707

3 commentaires

Thanks a Lot Bruno Luong Yes I was looking for this. Just another question related to.
In the Above code we have a variable "compareStrings", but what if I do not have those strings in there but want to group the headers that are same?
For Example: In the sample file I had 4 different kind of headers and in compareStrings I had listed three. But if I have lets say 10 headers and I do not intend to list or change my compareStrings variable how could I run that?
Thank you again
You can set compareStrings = unique(header); then continue as the rest
A=readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1460327/Book1.xlsx');
header=A.Var1;
compareStrings = unique(header);
% the do the same thing
C = cell(size(compareStrings));
for k=1:length(compareStrings)
C{k} = A(strcmp(header, compareStrings{k}),:);
end
C{:}
ans = 4×3 table
Var1 Var2 Var3 _________ ________ ________ {'ABCDF'} 0.52474 0.85068 {'ABCDF'} 0.14364 0.25306 {'ABCDF'} 0.030389 0.068798 {'ABCDF'} 0.6799 NaN
ans = 12×3 table
Var1 Var2 Var3 ____________ ________ _______ {'ALLLDKKD'} 0.81691 0.90177 {'ALLLDKKD'} 0.821 0.48899 {'ALLLDKKD'} 0.01612 0.92917 {'ALLLDKKD'} 0.51538 0.25401 {'ALLLDKKD'} 0.60644 0.70253 {'ALLLDKKD'} 0.85535 0.18184 {'ALLLDKKD'} 0.084649 0.5842 {'ALLLDKKD'} 0.33199 0.22169 {'ALLLDKKD'} 0.37172 0.52223 {'ALLLDKKD'} 0.17652 0.7413 {'ALLLDKKD'} 0.083156 0.75819 {'ALLLDKKD'} 0.70004 0.73707
ans = 20×3 table
Var1 Var2 Var3 ______________ ________ _________ {'AYYSBSABDB'} 0.18947 0.41952 {'AYYSBSABDB'} 0.87988 0.84733 {'AYYSBSABDB'} 0.044079 0.67988 {'AYYSBSABDB'} 0.68672 0.13665 {'AYYSBSABDB'} 0.73377 0.8584 {'AYYSBSABDB'} 0.43717 0.19983 {'AYYSBSABDB'} 0.37984 0.60734 {'AYYSBSABDB'} 0.97966 0.54304 {'AYYSBSABDB'} 0.39899 0.16232 {'AYYSBSABDB'} 0.44019 0.0056531 {'AYYSBSABDB'} 0.15681 0.77149 {'AYYSBSABDB'} 0.32603 0.76479 {'AYYSBSABDB'} 0.31406 0.42107 {'AYYSBSABDB'} 0.8945 0.056813 {'AYYSBSABDB'} 0.24702 0.58575 {'AYYSBSABDB'} 0.31068 0.17416
ans = 14×3 table
Var1 Var2 Var3 __________ _______ _______ {'defggt'} 0.80452 0.55857 {'defggt'} 0.12369 0.35813 {'defggt'} 0.6379 0.25596 {'defggt'} 0.89595 0.46676 {'defggt'} 0.54452 0.43122 {'defggt'} 0.76044 0.40233 {'defggt'} 0.38287 0.85625 {'defggt'} 0.73387 0.37358 {'defggt'} 0.83975 0.21899 {'defggt'} 0.82822 0.43342 {'defggt'} 0.12952 0.07045 {'defggt'} 0.87132 0.91706 {'defggt'} 0.7532 0.18353 {'defggt'} 0.55729 NaN
or split without loop
A=readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1460327/Book1.xlsx');
header=A.Var1;
[compareStrings, ~, J] = unique(header); % compareStrings is no needed for the rest
[Js, is] = sort(J);
n = diff(find([true; diff(Js); true]));
C = mat2cell(A(is,:), n, size(A,2));
C{:}
ans = 4×3 table
Var1 Var2 Var3 _________ ________ ________ {'ABCDF'} 0.52474 0.85068 {'ABCDF'} 0.14364 0.25306 {'ABCDF'} 0.030389 0.068798 {'ABCDF'} 0.6799 NaN
ans = 12×3 table
Var1 Var2 Var3 ____________ ________ _______ {'ALLLDKKD'} 0.81691 0.90177 {'ALLLDKKD'} 0.821 0.48899 {'ALLLDKKD'} 0.01612 0.92917 {'ALLLDKKD'} 0.51538 0.25401 {'ALLLDKKD'} 0.60644 0.70253 {'ALLLDKKD'} 0.85535 0.18184 {'ALLLDKKD'} 0.084649 0.5842 {'ALLLDKKD'} 0.33199 0.22169 {'ALLLDKKD'} 0.37172 0.52223 {'ALLLDKKD'} 0.17652 0.7413 {'ALLLDKKD'} 0.083156 0.75819 {'ALLLDKKD'} 0.70004 0.73707
ans = 20×3 table
Var1 Var2 Var3 ______________ ________ _________ {'AYYSBSABDB'} 0.18947 0.41952 {'AYYSBSABDB'} 0.87988 0.84733 {'AYYSBSABDB'} 0.044079 0.67988 {'AYYSBSABDB'} 0.68672 0.13665 {'AYYSBSABDB'} 0.73377 0.8584 {'AYYSBSABDB'} 0.43717 0.19983 {'AYYSBSABDB'} 0.37984 0.60734 {'AYYSBSABDB'} 0.97966 0.54304 {'AYYSBSABDB'} 0.39899 0.16232 {'AYYSBSABDB'} 0.44019 0.0056531 {'AYYSBSABDB'} 0.15681 0.77149 {'AYYSBSABDB'} 0.32603 0.76479 {'AYYSBSABDB'} 0.31406 0.42107 {'AYYSBSABDB'} 0.8945 0.056813 {'AYYSBSABDB'} 0.24702 0.58575 {'AYYSBSABDB'} 0.31068 0.17416
ans = 14×3 table
Var1 Var2 Var3 __________ _______ _______ {'defggt'} 0.80452 0.55857 {'defggt'} 0.12369 0.35813 {'defggt'} 0.6379 0.25596 {'defggt'} 0.89595 0.46676 {'defggt'} 0.54452 0.43122 {'defggt'} 0.76044 0.40233 {'defggt'} 0.38287 0.85625 {'defggt'} 0.73387 0.37358 {'defggt'} 0.83975 0.21899 {'defggt'} 0.82822 0.43342 {'defggt'} 0.12952 0.07045 {'defggt'} 0.87132 0.91706 {'defggt'} 0.7532 0.18353 {'defggt'} 0.55729 NaN
or if you want them in a same table
A=readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1460327/Book1.xlsx');
[~,is] = sort(A.Var1);
B = A(is,:)
B = 50×3 table
Var1 Var2 Var3 ____________ ________ ________ {'ABCDF' } 0.52474 0.85068 {'ABCDF' } 0.14364 0.25306 {'ABCDF' } 0.030389 0.068798 {'ABCDF' } 0.6799 NaN {'ALLLDKKD'} 0.81691 0.90177 {'ALLLDKKD'} 0.821 0.48899 {'ALLLDKKD'} 0.01612 0.92917 {'ALLLDKKD'} 0.51538 0.25401 {'ALLLDKKD'} 0.60644 0.70253 {'ALLLDKKD'} 0.85535 0.18184 {'ALLLDKKD'} 0.084649 0.5842 {'ALLLDKKD'} 0.33199 0.22169 {'ALLLDKKD'} 0.37172 0.52223 {'ALLLDKKD'} 0.17652 0.7413 {'ALLLDKKD'} 0.083156 0.75819 {'ALLLDKKD'} 0.70004 0.73707

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by