Chain elements in matrix using rows - arrays

I have a matrix where each row is a combination of two numbers, like A = [1 2; 2 5; 3 4; 4 6; 5 6]
A is built so that, for each row, the first elements is always smaller than the second one.
I need to return, from A, the lists of chained elements (in the case above, the lists of chained elements are 1 2 5 6 and 3 4 6). These lists are essentially built by considering a row, and checking is the last number is the first number of another row. Do you have any suggestion on how to do this?

If I got the question correctly, assuming A as the input array, you can use bsxfun -
mask = bsxfun(#eq,A(:,1),A(:,2).');
out = unique(A(any(mask,1).' | any(mask,2),:))
Sample run -
>> A
A =
1 2
3 4
2 5
5 6
>> mask = bsxfun(#eq,A(:,1),A(:,2).');
>> unique(A(any(mask,1).' | any(mask,2),:))
ans =
1
2
5
6
You can also use ismember, like so -
out = unique(A(ismember(A(:,1),A(:,2)) | ismember(A(:,2),A(:,1)),:))
Third option would be to use intersect to solve it, like so -
[~,idx1,idx2] = intersect(A(:,1),A(:,2));
out = unique(A([idx1,idx2],:))

The following seems to work. It builds a matrix (B) that tells which elements are connected (by 1 step). It then extends that matrix ( C) to include 0-step, 1-step, ..., (n-1)-step connections, where n is the number of nodes.
From that matrix, groups of connected elements are obtained (R). Finally, only "maximal" groups are kept (that is, those, not contained in other groups).
A = [1 2; 3 4; 2 5; 4 6; 5 6]; %// data
n = max(A(:));
B = full(sparse(A(:,1), A(:,2), 1, n, n )); %// matrix of 1-step connections
C = eye(n) | B; %// initiallize with 0-step and 1-step connections
for k = 1:n-1
C = C | C*B; %// add k-step connections, up to k=n-1
end
[ii, jj] = find(C);
R = accumarray(ii, jj, [], #(x) {sort(x).'}); %'// all groups (maximal or not)
[xx, yy] = ndgrid(1:n);
C = cellfun(#(x,y) all(ismember(x, y)), R(xx), R(yy) ); %// group included in another?
result = R(all(~C | eye(n), 2)); %// keep only groups that are not included in others
This gives
>> result{:}
ans =
1 2 5 6
ans =
3 4 6

Related

MATLAB: Sort a 3d array by another 3d array

I have a 3d array A of random numbers which I'd like to order each k'th dimension individually:
A=rand(3,1,16);
[m, n, k]=size(A);
The array that dictates the order of each matrix in the 3rd dimension is B:
B=randi(3,3,1,16); %this should be without replacement but think it will work anyway
If A(:,1,1)=[0.5, 0.2, 0.6]' and B(:,1,1)=[3,1,2] then the ordered A should be [0.2, 0.6, 0.5]' and so on for each A(:,1,1:k). Please note it's not ordering A numerically.
A(B) is what I might have expected to work and keeps the dimensions but not the right orders.
I've tried to work through this: https://uk.mathworks.com/matlabcentral/answers/307838-sort-3d-matrix-according-to-another-3d-matrix without any success.
Any thoughts would be much appreciated.
To sort A along the k-th dimension based on B: if your data is not complex, a simple way is:
Pack A and B into a complex array where A is the imaginary part and B is the real part;
Sort along the k-th dimension based on the real part;
Keep the imaginary part.
This is done in one line as follows:
result = imag(sort(B+1j*A, k, 'ComparisonMethod', 'real'));
Example:
>> A = rand(2,4,3)
A(:,:,1) =
0.162182308193243 0.311215042044805 0.165648729499781 0.262971284540144
0.794284540683907 0.528533135506213 0.601981941401637 0.654079098476782
A(:,:,2) =
0.689214503140008 0.450541598502498 0.228976968716819 0.152378018969223
0.748151592823709 0.083821377996933 0.913337361501670 0.825816977489547
A(:,:,3) =
0.538342435260057 0.078175528753184 0.106652770180584 0.004634224134067
0.996134716626885 0.442678269775446 0.961898080855054 0.774910464711502
>> B = randi(9, size(A))
B(:,:,1) =
8 1 3 4
8 4 8 9
B(:,:,2) =
2 2 8 5
3 2 6 2
B(:,:,3) =
8 4 4 3
6 5 1 2
>> k = 2;
>> result = imag(sort(B+1j*A, k, 'ComparisonMethod', 'real'))
result(:,:,1) =
0.311215042044805 0.165648729499781 0.262971284540144 0.162182308193243
0.528533135506213 0.601981941401637 0.794284540683907 0.654079098476782
result(:,:,2) =
0.450541598502498 0.689214503140008 0.152378018969223 0.228976968716819
0.083821377996933 0.825816977489547 0.748151592823709 0.913337361501670
result(:,:,3) =
0.004634224134067 0.078175528753184 0.106652770180584 0.538342435260057
0.961898080855054 0.774910464711502 0.442678269775446 0.996134716626885

How to obtain the index of a line specific value [ a b c d] within a double matrix?

I have made a table composed of four variables[v1, v2, v3, v4], with near 26000 rows. I need to search the values of an specific line e.g. [1 1 2 2016] within the table (26000 x 4), and return the index of the line in which the search is located.
Example of what I would like to search:
want_1 = [1 1 3 2016];
want_2 = [1 1 5 2016];
And would like to obtain the number of the line in which it is located.
If you have a matrix M (which you could get from table2array(T) on your table) you should be able to use implicit expansion* and all to get your result
srch = [1 1 3 2016]; % Row to search for
res = find( all( M == srch, 2 ) );
The find converts the logical array returned by all into the row numbers where it is true.
The implicit expansion here is basically the same as repeating the srch array for the entire height of the matrix M and then doing an element-wise == operation. The all then ensures that every comparison in a given row was true (i.e. a match for every element of srch).
*Implicit expansion relies on having MATLAB R2016b or newer... for older versions you can achieve the same using bsxfun.
Just as an exercise in alternatives, you could use splitapply instead to apply the all and == operators to each row in turn, this is probably slower...
res = find( splitapply( #(x)all(x==srch), M, (1:size(M,1)).' ) );
Or you could even use rowfun, which is a bit of a loop-in-disguise, but would work on your table T without having to first convert it to a matrix:
res = find( rowfun( #(varargin)all([varargin{:}]==srch), T, 'OutputFormat', 'uniform' ) );
For a matrix, you can use ismember with the 'rows' option:
M = [1 2 3 4; 1 1 3 2016; 5 6 7 8]; % example data matrix
wanted = [1 1 3 2016]; % example wanted row
result = find(ismember(M, wanted, 'rows'));
This also works with a table, as long as the wanted row is a table (of one row) with the same variable names:
M = table;
M.hour = [1; 2; 3]; M.day = [4; 5; 6]; M.month = [7; 8; 9]; M.year = [10; 11; 12];
wanted = table;
wanted.hour = 2; wanted.day = 5; wanted.month = 8; wanted.year = 11;
result = find(ismember(M, wanted, 'rows'));

Collapsing matrix into columns

I have a 2D matrix where the № of columns is always a multiple of 3 (e.g. 250×27) - due to a repeating organisation of the results (A,B,C, A,B,C, A,B,C, and so forth). I wish to reshape this matrix to create a new matrix with 3 columns - each containing the aggregated data for each type (A,B,C) (e.g. 2250×3).
So in a matrix of 250×27, all the data in columns 1,4,7,10,13,16,19,22,25 would be merged to form the first column of the resulting reshaped matrix.
The second column in the resulting reshaped matrix would contain all the data from columns 2,5,8,11,14,17,20,23,26 - and so forth.
Is there a simple way to do this in MATLAB? I only know how to use reshape if the columns I wanted to merge were adjacent (1,2,3,4,5,6) rather than non-adjacent (1,4,7,10,13,16) etc.
Shameless steal from #Divakar:
B = reshape( permute( reshape(A,size(A,1),3,[]), [1,3,2]), [], 3 );
Let A be your matrix. You can save every third column in one matrix like:
(Note that you don't have to save them as matrices separately but it makes this example easier to read).
A = rand(27); %as test
B = A(:,1:3:end);
C = A(:,2:3:end);
D = A(:,3:3:end);
Then you use reshape:
B = reshape(B,[],1);
C = reshape(C,[],1);
D = reshape(D,[],1);
And finally put it all together:
A = [B C D];
You can just treat every set of columns as a single item and do three reshapes together. This should do the trick:
[save as "reshape3.m" file in your Matlab folder to call it as a function]
function out = reshape3(in)
[~,C]=size(in); % determine number of columns
if mod(C,3) ~=0
error('ERROR: Number of rows must be a multiple of 3')
end
R_out=numel(in)/3; % number of rows in output
% Reshape columns 1,4,7 together as new column 1, column 2,5,8 as new col 2 and so on
out=[reshape(in(:,1:3:end),R_out,1), ...
reshape(in(:,2:3:end),R_out,1), ...
reshape(in(:,3:3:end),R_out,1)];
end
Lets suppose you have a 3x6 matrix A
A = [1 2 3 4 5 6;6 5 4 3 2 1;2 3 4 5 6 7]
A =
1 2 3 4 5 6
6 5 4 3 2 1
2 3 4 5 6 7
you extract the size of the matrix
b =size(A)
and then extract each third column for a single row
c1 = A((1:b(1)),[1:3:b(2)])
c2 = A((1:b(1)),[2:3:b(2)])
c3 = A((1:b(1)),[3:3:b(2)])
and put them in one matrix
A_result = [c1(:) c2(:) c3(:)]
A_result =
1 2 3
6 5 4
2 3 4
4 5 6
3 2 1
5 6 7
My 2 cents:
nRows = size(matrix, 1);
nBlocks = size(matrix, 2) / 3;
matrix = reshape(matrix, [nRows 3 nBlocks]);
matrix = permute(matrix, [1 3 2]);
matrix = reshape(matrix, [nRows * nBlocks 1 3]);
matrix = reshape(matrix(:), [nRows * nBlocks 3]);
Here's my 2 minute take on it:
rv = #(x) x(:);
ind = 1:3:size(A,2);
B = [rv(A(:,ind)) rv(A(:,ind+1)) rv(A(:,ind+2))];
saves a few ugly reshapes, may be a bit slower though.
If you have the Image Processing Toolbox, im2col is a very handy solution:
out = im2col(A,[1 4], 'distinct').'
Try Matlab function mat2cell, I think this form is allowed.
X is the "start matrix"
C = mat2cell(X, [n], [3, 3, 3]); %n is the number of rows, repeat "3" as many times as you nedd
%extract every matrix
C1 = C{1,1}; %first group of 3 columns
C2 = C{1,2}; %second group of 3 columns
%repeat for all your groups
%join the matrix with vertcat
Cnew = vertcat(C1,C2,C3); %join as many matrix n-by-3 as you have

Count items in one array in another array matlab

Given the values in C, I want to count how many times I encounter each of these values in the vector B. For example, if C = [1 2], I'd like to count how many "1"s are in B and how many "2"s are in B by checking the elements of array C.
However, the code I have seems to count up to 5, but what the true output should be is that the number of 1s is 2 and the number of 2s is 3.
The code I wrote is shown below. Any help would be certainly appreciated.
My code :
B = [ 1 2 1 4 5 2 2 ]
C = [ 1 2 ]
lb = length(B)
lc = length(C)
for i = 1:length(B)
for j = 1:length(C)
if B(1,i)==C(1,j)
c=c+1
A(i)=c
end
end
end
Santhan Salai gave the corrected answer if you want to stick with loops. However, for efficiency, you could achieve what you want with bsxfun and sum if you don't want to loop:
>> B = [ 1 2 1 4 5 2 2 ];
>> C = [ 1 2 ];
>> D = sum(bsxfun(#eq, B, C.'), 2)
D =
2
3
bsxfun is a nice tool that allows you to broadcast data across singleton dimensions. In our case, I used the equals functions to check for element-wise equality and I transposed C so that it becomes a column vector. What will be produced is a matrix where we have as many rows as there are in C and as many columns as there are in B. Each row of this output matrix tells you which elements were equal to each corresponding element in C. C is broadcasted such that we get a matrix of a bunch of [1;2] columns mashed together for as many elements as there are in B and B is broadcasted such that we get replicates of B with as many rows as there are in C. By doing element-wise equality, we would produce a logical matrix where each element for a row tells you which elements were equal to the corresponding value of C. All you have to do now is sum over the columns to get how many elements were equal to each value in C, hence the sum call over the second dimension.
If you want overkill, you can also use accumarray. Assuming that B and C only consist of integers, do:
>> B = [ 1 2 1 4 5 2 2 ];
>> C = [ 1 2 ];
>> D = accumarray(B.', 1);
D =
2
3
0
1
1
This however computes the frequency of all unique values in B, and so you'd just want the first two entries in D.
One more alternative is to use histc:
>> B = [ 1 2 1 4 5 2 2 ];
>> C = [ 1 2 ];
>> D = histc(B, C)
D =
2 3
What you are basically computing is a histogram of B at specified bins, which are denoted at C.
Using arrayfun could be one alternate to your loop
out = arrayfun(#(x) sum(ismember(B,x)), C)
Here is your corrected loop
count = zeros(1,numel(C));
for ii = 1:numel(C)
for jj = 1:numel(B)
if (C(ii) == B(jj))
count(ii) = count(ii) + 1;
end
end
end

Element-wise array replication in Matlab

Let's say I have a one-dimensional array:
a = [1, 2, 3];
Is there a built-in Matlab function that takes an array and an integer n and replicates each
element of the array n times?
For example calling replicate(a, 3) should return [1,1,1,2,2,2,3,3,3].
Note that this is not at all the same as repmat. I can certainly implement replicate by doing repmat on each element and concatenating the result, but I am wondering if there is a built in function that is more efficient.
I'm a fan of the KRON function:
>> a = 1:3;
>> N = 3;
>> b = kron(a,ones(1,N))
b =
1 1 1 2 2 2 3 3 3
You can also look at this related question (which dealt with replicating elements of 2-D matrices) to see some of the other solutions involving matrix indexing. Here's one such solution (inspired by Edric's answer):
>> b = a(ceil((1:N*numel(a))/N))
b =
1 1 1 2 2 2 3 3 3
a = [1 2 3];
N = 3;
b = reshape(repmat(a,N,1), 1, [])
As of R2015a, there is a built-in and documented function to do this, repelem:
repelem Replicate elements of an array.
W = repelem(V,N), with vector V and scalar N, creates a vector W where each element of V is repeated N times.
The second argument can also be a vector of the same length as V to specify the number of replications for each element. For 2D replication:
B = repelem(A,N1,N2)
No need for kron or other tricks anymore!
UPDATE: For a performance comparison with other speedy methods, please see the Q&A Repeat copies of array elements: Run-length decoding in MATLAB.
>> n=3;
>> a(floor((0:size(a,2)*n-1)/n)+1)
ans =
1 1 1 2 2 2 3 3 3
Some exotic alternatives. Admittedly more funny than useful:
Assign the (first) result of meshgrid to a vector:
b = NaN(1,numel(a)*n); %// pre-shape result
b(:) = meshgrid(a,1:n);
Build a matrix that multiplied by a gives the result:
b = a * fliplr(sortrows(repmat(eye(numel(a)),n,1))).';
Use ind2sub to generate the indices:
[~, ind] = ind2sub([n 1],1:numel(a)*n);
b = a(ind);
If you have the image processing toolbox, there is another alternative:
N = 3;
imresize(a, [1 N*numel(a)],'nearest')
% To get b = [1 1 1 2 2 2 3 3 3]
N = 3;
a = [1 2 3];
temp_a = a(ones(N,1),:);
b = reshape(temp_a,1,numel(temp_a));
% To get b = [1 2 3 1 2 3 1 2 3]
N = 3;
a = [1 2 3];
temp_a = a(ones(N,1),:);
b = reshape(temp_a',1,numel(temp_a));

Resources