필터 지우기
필터 지우기

Comparing two matrices to find and eliminate similarities?

조회 수: 1 (최근 30일)
Calabrese
Calabrese 2017년 7월 7일
댓글: Calabrese 2017년 7월 17일
This is a simplified example. I have two tables created by pulling info from multiple files, file names and size..
%Grab Folder from directory, class(char)
dir_QAR1 = uigetdir();
% Folder info, class(struct), names, dates, bytes, etc.
QAR1 = dir(dir_QAR1);
length_QAR1 = length(QAR1);
%Repeat
dir_QAR2 = uigetdir();
QAR2 = dir(dir_QAR2);
length_QAR2 = length(QAR2);
Extract file names, class(cell)
files_QAR1 = {QAR1.name}';
files_QAR2 = {QAR2.name}';
%Extract file sizes, class(cell)
bytes_QAR1 = {QAR1.bytes}';
bytes_QAR2 = {QAR2.bytes}';
%create a table of file names and sizes, class(cell)
package_QAR1 = [files_QAR1, bytes_QAR1];
package_QAR2 = [files_QAR2, bytes_QAR2];
%Output
package_QAR1 =
'.' [ 0]
'..' [ 0]
'CM38.xps' [ 446756]
'CM39_005.xls' [ 134144]
'CM76.xps' [ 287673]
'CM37_002.pptx' [ 1860347]
'CM96.xps' [ 291920]
%...many more rows
Example tables, not actual output. Referencing package_QAR1 and package_QAR2
% File Name, File Size
package_QAR1 =
'a' [1]
'b' [2]
'c' [3]
'd' [4]
'e' [5]
package_QAR2 =
'a' [1]
'b' [2]
'c' [4]
'd' [4]
'x' [1]
'e' [5]
'f' [6]
'g' [7]
'h' [8]
I would like to compare table data. If name (a) has the same size (1) in both tables, delete field in package_QAR2. Output new table (X) showing items that had the same name but different size. Should return a table with the following info (not actual outputs)..
package_QAR2_2 =
'c' [4]
'x' [1]
'f' [6]
'g' [7]
'h' [8]
X =
'c' [4]
It is important to note that file names are not in any consistent order and table lengths vary.
Please note the example tables aren't actual outputs, they are included to merely reference data being kept/ removed.
  댓글 수: 6
Calabrese
Calabrese 2017년 7월 10일
편집: Calabrese 2017년 7월 10일
I apologize for the confusion, I hope it is more clear now. Please note the example tables aren't actual outputs, they are included to merely reference data being kept/ removed.
Jan
Jan 2017년 7월 11일
@Calabrese: I admit that I'm completely confused now. Are the input data table objects or cells? Could you please post some code, which creates the wanted input by copy&pasting it to the command window?

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2017년 7월 10일
편집: Jan 2017년 7월 11일
When I ignore the ambiguities, perhaps you need something like this:
A = {'a', 1;
'b', 2;
'c', 3;
'd', 4;
'e', 5};
B = {'a', 1;
'b', 2;
'c', 4;
'd', 4;
'x', 1;
'e', 5;
'f', 6;
'g', 7;
'h', 8};
[LiB, iA] = ismember(B(:, 1), A(:, 1));
match = (cat(1, A{iA(iA~=0), 2}) == cat(1, B{LiB, 2}));
index = find(LiB);
C = B(index(~match), :);
B(index(match), :) = [];
  댓글 수: 7
Jan
Jan 2017년 7월 13일
@Calabrese: Note that I cannot run your code, because the files are not existing on my computer. Therefore you are the only person who can debug the code.
what1 = cellfun('size', package_QAR1(ipackage_QAR1(ipackage_QAR1~=0), 2), 1);
what2 = cellfun('size', package_QAR1(ipackage_QAR1(ipackage_QAR1~=0), 2), 2);
what3 = cellfun('size', package_QAR2(Lipackage_QAR2, 2), 1);
what4 = cellfun('size', package_QAR2(Lipackage_QAR2, 2), 2);
all(what1 == what1(1))
all(what2 == what2(1))
all(what3 == what3(1))
all(what4 == what4(1))
Does this really reply TRUE 4 times?
Try to find out which of the cat() command fails:
cat(1, package_QAR1{ipackage_QAR1(ipackage_QAR1~=0), 2})
cat(1, package_QAR2{Lipackage_QAR2, 2})
Ah, well, I see, that you apply @num2str to the data. Then the 2nd column are not scalar doubles, but char vectors of different size:
A = {'a', '1';
'b', '12'};
not
A = {'a', 1;
'b', 12};
Correct? Then the concatenation must fail.
What is the prupose of this:
cellfun(@num2str,package_QAR1,'UniformOutput',0);
Note that this changes the type of the inputs again. I'm asking you for 6 days repeatedly for the correct type of the inputs. And after posting an answer and discussing in the comments, you change the input type again compared to the text of the question. This is not a useful strategy to solve a problem.
I suggest to omit the cellfun(@num2str).
The code would look much cleaner, if you move my suggested code inside a function and keep the lean names for the variables.
Calabrese
Calabrese 2017년 7월 17일
Your original solution worked. I had changed my original code in the beginning hoping it might bring me to a solution which prevented your solution from working. I made the changes and it works like a charm. This is extremely helpful, thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by