Azzera filtri
Azzera filtri

Comparing two matrices to find and eliminate similarities?

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Calabrese
Calabrese il 10 Lug 2017
Modificato: Calabrese il 10 Lug 2017
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 il 11 Lug 2017
@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?

Accedi per commentare.

Risposta accettata

Jan
Jan il 10 Lug 2017
Modificato: Jan il 11 Lug 2017
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 Commenti
Jan
Jan il 13 Lug 2017
@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 il 17 Lug 2017
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Type Identification in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by