Having each row compare its string with other rows within the problem and getting corresponding data relative to the string in other columns.
조회 수: 4 (최근 30일)
이전 댓글 표시
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
Stephen23
2023년 8월 20일
편집: Stephen23
2023년 8월 20일
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.
채택된 답변
Bruno Luong
2023년 8월 20일
편집: Bruno Luong
2023년 8월 20일
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{:}
댓글 수: 3
Bruno Luong
2023년 8월 20일
편집: Bruno Luong
2023년 8월 20일
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{:}
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{:}
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,:)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!