Use Matlab to compare values in different columns of 2 cell arrays - matlab

I'm trying to compare two cell arrays that contain both characters and numbers. I would like to compare specific columns and then return the values in another related column.
For example, I have two cell arrays of the forms:
One= Two=
[A X 2 10 [A 1 X 2 76
B Y 2 11 B 1 Y 2 78
A X 5 22 C 1 Z 2 80
B Y 5 23 D 1 X 4 98
A X 6 28 E 1 Y 4 99
B Y 6 28 F 1 Z 4 100
C Z 6 28] G 1 X 6 110
H 1 Y 6 120]
And I want to be able to find everywhere column 2 and 3 of 'One' equals column 3 and 4 of 'Two' and return the specific value in column 5 of 'Two' (and ideally also the value in column 3). In this example, 'One' has an X 2, Y 2, X 6, and Y 6 in common with 'Two' so I would obtain a result that is:
Three=
[X 76
Y 78
X 110
Y 120]
The cell arrays I currently have are also of different sizes. Any help would be appreciated.

You can declare One and Two as cell array
Also Create one empty cell named Third. You can compare and get output as
[m n]=size(One)
Three = {};
for i=1:m
if ((One{i,2} == Two{i,3}) && (One{i,3} == Two{i,4}) )
Three=[Three; {One{i,2},Two{i,5}}];
end
end

Related

Calculate minimum and maximum values of each variable in a table in kdb

Consider the following table:
sym A B
X 1 2
Y 4 1
X 6 9
Z 6 3
Z 3 7
Y 1 8
I want to find the minimum A value and maximum B value for each of my symbols X, Y & Z and display them in a new table, i.e.
sym minA maxB
X 1 9
Y 1 8
Z 3 7
Thanks.
This should do it;
select minA:min A, maxB:max B by sym from table

Use Matlab to compare columns in different cell arrays

I'm trying to compare two cell arrays that contain both characters and numbers. I would like to compare two specific columns and then return the values in another related column.
For example, I have two cell arrays of the forms:
One= Two=
[A 2 10 [A 1 2 76
B 2 11 B 1 2 78
A 5 22 C 1 2 80
B 5 23 D 1 4 98
A 6 28 E 1 4 99
B 6 28 F 1 4 100
C 6 28] G 1 6 110]
And I want to be able to find everywhere column 2 of 'One' equals column 3 of 'Two' and return the specific value in column 4 of 'Two.' So for this example, I would obtain a result that is:
Three=
[76
78
80
110]
Any help would be appreciated.
Option 1: convert to numeric array first
X = cell2mat(One(:,2:end));
Y = cell2mat(Two(:,2:end));
result = Y(X(:,1)==Y(:,2),3)
Option 2: convert to numeric array at various points
result = cell2mat(Two(cell2mat(One(:,2))==cell2mat(Two(:,3)),4))
Option 3: convert cell to table first
T1 = cell2table(One);
T2 = cell2table(Two);
result = T2.Two4(T1.One2==T2.Two3)
Option 4: abuse how Matlab cell arrays and numeric arrays work
result = [Two{([One{:,2}]==[Two{:,3}])',4}]'

how to assign values to a three dimension matrix

I give a small example: matrix A[3*3*2], only 9 elements of matrix A are '1',other elements of A are '0'. (the value of '1' and '0' do not matter, just indicate the number of voxels, and following 5 values belong to the 9 elements of matrix A.
matrix B = [ 12
34
61
81
42 ];
matrix C = [ 1 2 1
1 1 1
2 3 1
2 2 1
3 1 1];
The 5 elements belong to the 9 elements of matrix A.
How to assign the values of matrix B to matrix A based on the matrix C?
Here's how to do that using linear indexing via sub2ind:
A(sub2ind(size(A),C(:,1),C(:,2),C(:,3)))=B;

matlab. copy values from one matrix based on values of another matrix

I have matrix a <500 x 500> and matrix b <500 x 2>.
Matrix b contains two types of values which are row and column coordinates for matrix a. I would like to use the values in matrix b to to copy all the values that fall on the row and column coordinates of matrix a.
see example below
matrix a matrix b output
1 2 3 4 5 1 5 1 2 3 4 5
6 7 8 9 10 2 5 7 8 9 10
11 12 13 14 15 1 3 11 12 13
Because every row will have a different length you'll need to save the values into a cell array.
Something like this should work:
output = cell( size(b,1),1);
for i = 1:size(a,1)
output{i} = a(i, b(i,1):b(i,2) )
end

Intersection of several sets of results corresponding to different events in matlab

I have a matrix (5x10000) with the fifth line contains values ​​between 1 and 50 corresponding to different events of an experiment. My goal is to find the columns of the matrix which are the same for different events. In other words, I want the columns results for all possible combinations of different events (subsets of {1,2, .., 50}) (For example: {1,3,7} and {7,1,3} are of course the same combination).
It sounds like a problem of intersection of sets that each contains all possible outcomes for a given event. I hope also that the computation time is reasonable.
example with a matrix (5x20):
A =
20 4 4 74 20 20 3 1 1 4 3 3 3 7 4 1 20 3 3 74
36 1 1 11 36 36 3 3 3 1 3 3 3 9 4 3 36 4 3 11
77 1 1 15 77 77 1 3 3 1 1 1 1 10 3 2 77 4 1 15
9 4 4 40 9 9 2 4 4 4 2 2 2 40 1 4 9 3 2 40
3 4 2 6 7 3 4 5 2 7 4 2 7 6 7 2 5 5 1 3
in this case we have seven different events from 1 to 7: line 5
for example:
the intersection of the results of events 3, 5 and 7 is the vector: [20 36 77 9]'
the intersection of the results of events 1, 2, 4 and 7 is the vector: [3 3 1 2]'
the intersection of the results of events 3 and 6 are the vectors: [20 36 77 9]' and [74 11 15 40]'
So what I want is the common columns for a specified number of different events between 1 and 50. For example, how to get the columns common to 20 different events? The problem becomes more complicated for me when I think to find this result for all possible combinations of 20 events in the set {1,2,..., 50}.
I want the common columns for all possible combination for a given number of different events, but I gave the number 20 just as an example on which to base one solution.
I'll rephrase my question that to make it clearer:
the following matrices are sub-matrix of A, each corresponding to a given event:
A1= [3;3;1;2;1]
A1 corresponds to results of the event 1
A2= [4 1 3 1;1 3 3 3;1 3 1 2;4 4 2 4;2 2 2 2]
A2 corresponds to results of the event 2
A3= [20 20 74;36 36 11;77 77 15;9 9 40;3 3 3]
A3 corresponds to results of the event 3
A4= [4 3 3;1 3 3;1 1 1;4 2 2;4 4 4]
A4 corresponds to results of the event 4
A5= [1 20 3;3 36 4;3 77 4;4 9 3;5 5 5]
A5 corresponds to results of the event 5
A6= [74 7;11 9;15 10;40 40;6 ]6
A6 corresponds to results of the event 6
A7= [20 4 3 4 7;36 1 3 4;77 1 1 3;9 4 2 1;7 7 7 7]
A7 corresponds to results of the event
my goal is to find intersection along the columns of the matrix Ai (1:4,:) i = 1,2, ... 7
in other words:
intersection(Ai,Aj)(1:4,:) for i and j different
intersection(Ai,Aj,Ak)(1:4,:) for i,j and k different
intersection(Ai,Aj,Ak,Al)(1:4,:) for i,j,k and l different
intersection(Ai,Aj,Ak,Al,Am)(1:4,:) for i,j,k,l and m different
intersection(Ai,Aj,Ak,Al,Am,An)(1:4,:) for i,j,k,l,m and n different
intersection(Ai,Aj,Ak,Al,Am,An,Ao)(1:4,:) for i,j,k,l,m,n and o different
intersection(Ai,Aj,Ak,Al,Am,An,Ao,Ap)(1:4,:) for i,j,k,l,m,n,o and p different
when I say "intersection (Ai, Aj)(1:4,:) for i and j different," I want the columns common to the matrix Ai(1:4,:) and Aj(1:4,:)
the result for each intersection can be many column vectors, not necessarily one, depending on the columns of the matrix A.
I hope that each result contains the vector column of the matrix Ai(1:4,:) followed by the corresponding values ​​of events, such as: if [3 3 1 2]' is the intersection of A1, A2, A4 and A7, I want to get as a result the vector [3 3 1 2 1 2 4 7]'
for example: intersection(A1,A2,A3,A4)(1:4,:): my goal is to avoid the following loop:
[n1 m1] = size(A1);
[n2 m2] = size(A2);
[n3 m3] = size(A3);
[n4 m4] = size(A4);
k=1;
for i1=1:m1
for i2=1:m2
for i3=1:m3
for i4=1:m4
if A1(1:4,i1)==A2(1:4,i2) && A2(1:4,i2)==A3(1:4,i3) && A3(1:4,i3)==A4(1:4,i4)
intersection1234(:,k) = [A1(1:4,i1);A1(5,i1);A2(5,i2);A3(5,i3);A4(5,i4)];
k=k+1;
end
end
end
end
end
If I understand correctly, you want to find columns whose events are different. Building on John Colby's answer:
n = 1e3
tic
% Simulate data
% (Here we've split off the 5th row into a separate variable)
data = randi(5, [4 n]);
exptEvents = randi(50, 1, n);
% Find repeats
[b,i,j] = unique(data', 'rows');
% Organize the indices of the repeated columns into a cell array
reps = arrayfun(#(x) find(j==x), 1:length(i), 'UniformOutput', false);
% Find events corresponding to these repeats
reps_Events = cellfun(#(x) exptEvents(x), reps, 'UniformOutput', false);
U = cellfun(#unique, reps_Events, 'UniformOutput', false);
repeat_counts = cellfun(#length, U);
k=20;
rep_data = b(repeat_counts>=k,:);
toc
U in the code above has in every cell a group (or "combination") of unique events. Each cell also corresponds to a unique data column. If you need something else, please give an example. rep_data contains results that repeat in k or more events.
Can't tell for certain without an example input and desired output, but I believe this is what you're trying to do:
n = 1e4
tic
% Simulate data
% (Here we've split off the 5th row into a separate variable)
data = randi(5, [4 n]);
exptEvents = randi(50, 1, n);
% Find repeats
[b,i,j] = unique(data', 'rows');
% Organize the indices of the repeated columns into a cell array
reps = arrayfun(#(x) find(j==x), 1:length(i), 'UniformOutput', false);
% Find events corresponding to these repeats
reps_Events = cellfun(#(x) exptEvents(x), reps, 'UniformOutput', false);
toc
Elapsed time is 0.084577 seconds.