Create an 2-D empty array in Matlab - matlab

I would like to use union in Matlab to merge a series of 1-by-2 arrays for example [1,2] to an empty 2-D array U, i.e., U = union(U,[1,2],'rows'); But I'm confused how to initialize U, because U=[] didn't work, no luck with U=[[],[]] either. I tried U = [U,[1,2]], it works only when the 1-by-2 arrays in the series are all unique, or I would have duplicate entries in U.

So you want to get all the unique 1x2 arrays in a 2-D matrix? First, create a big 2-D array of all 1x2 arrays (including duplicates), then call unique.
C = unique(A,'rows')
As for using union, there is no way to have a empty 1x2 array. What you can do instead is initialize U to be the same as your first 1x2 array. Then loop over all the other 1x2 arrays and use union to build your output.
U = union(U, [1,2], 'rows')

Related

How to sum up values in corresponding indexes of cells?

I have a 1x24 cell array called chaining, whose each cell contains a 119x119 matrix:
I want to find the sum of each corresponding elements of all the cells, and store them in a matrix called result. That is, the (j,k)th element of result should contains the sum of the (j,k)th elements of all the matrices in the cell array chaining.
The code I wrote to do this is:
for j=1:size(chaining,2)
for k=1:size(chaining,2)
result(j,k) = sum(chaining{1,:}(j,k));
end
end
But this gives error because apparently MATLAB can't aggregate cell arrays for some reason (i.e., the chaining{1,:} part).
Could anyone please show me how to go about doing this?
how about
result = sum( cat(3, chaining{:}), 3 );
What just happened here?
First, we convert the cell array into a 3D array by "stacking" the 2D cell elements on the third dimension:
cat(3, chaining{:})
Once we have a 3D array of size 119-by-119-by-24 we can sum along the third dimension and get result of size 119-by-119:
sum( ..., 3 );
For more info see cat and sum help pages.
BTW,
If you insist on chaining{1,:}(jj,kk) type of solution (not recommended), you might find subsref command useful.

convert 3d cell array in 2d cell array

I transformed a structure S (with 13 fields and 96 rows, some fields are made of numbers, others of strings) into a cell array:
myCell= struct2cell(S);
So, I obtained a 3d cell array myCell 13x1x96, and I would like to transform it in a 2d cell array 96x13. Any suggestion is welcome!
A more general solution than what was suggested would employ the permute function:
B = permute(A,order) rearranges the dimensions of A so that they are in the order specified by the vector order.
...
permute and ipermute are a generalization of transpose (.') for multidimensional arrays.
In your case, running the command new_Cell = permute(myCell,[3,1,2]) would make the 13x1x96 96x13. As you can see, permute removes trailing singleton dimensions (resembling squeeze).
(Tested on MATLAB 2015b)

How to get multiple outputs of a function in a vector?

Say I have a function whose outputs are two reals a and b
[a,b]=function(c)
I'd like to get all the outputs in a vector v.
v=function(c) doesn't do what I want, v is 'a' only.
Of course here I could do v=[a,b].
But the function in question is ind2sub for a N-D array so it gives n outputs that I'd like to have in a vector directly.
Is there a way to do it?
Thanks very much!
You can use a cell array and a comma-separated list like so:
X = cell(N, 1);
[X{:}] = function(C);
The syntax X{:} is in fact expanded to [X{1}, X{2}, ...], which provides a valid sink for your function. As a result, each output variable will be stored in a different cell in X.
If each output variable is a scalar, you can flatten out the cell array into a vector by using yet another comma-separated list expansion:
v = [X{:}];

How to do intersection of arrays of unknown size which are stored in cells of a matrix?

I have a matrix of cells, call it M. The matrix dimensions are n^3.
Each cell contains an array of indices (a result of a regexp query on some string, not that it matters).
I want to intersect the indices in the arrays in each cell of M.
How can I do that? If I use intersection function how does it know to take the indices from inside the arrays in each cell?
As I understand I have to use cells because the inner arrays are of unknown size.
Is this what you want to do?
A = M{1};
for i = 2:numel(M)
A = intersect(A, M{i});
end
I don't think there's a neat way to do this using a single intersect call, or with e.g. cellfun.
If you only want the intersection of specific indices, you can do:
A = indices(1);
for i = indices(2:end)
A = intersect(A, M{i});
end

MATLAB: index a cell array with cell array of arrays and return a cell array

Say I have a cell array of (n X 1) vectors, A, and a cell array of vectors containing indices into A, called B. I wish to extract a cell array, C, such that C{i} = [A{B{i}}].
In other words, I have a cell array of arrays of indices, and I want to pull out the matrices corresponding to the concatenations of the vectors in A indexed by each of those arrays of indices.
for i = 1:length(B)
%# B{i} is an array of indices, C{i} is a matrix
C{i} = [ A{ B{i} } ];
end
The loop is equivalent to:
C = cellfun(#(x)[A{x}],B,'UniformOutput',false); %# implicit for loop w/ closure
Can I do that using an indexing expression alone? Or at least without the loop?
I think deal() might have to be involved but can't figure it out.
Here are two alternative solutions:
Collect all the indices of B together with the function cell2mat, index the contents of A to make one large matrix, then divide that matrix up using the function mat2cell and the sizes of the index arrays in B:
N = size(A{1}); % Size of an array in A
M = cellfun('prodofsize', B); % Array of sizes of elements in B
C = mat2cell([A{cell2mat(B)}], N, M);
Here's a more compact version of your cellfun-based solution:
C = cellfun(#(x) {[A{x}]}, B);
Ultimately, I would decide what solution to use based on speed and readability, which may actually turn out to be your for-loop-based solution.
Try the following expression:
C = A(cell2mat(B))
You may have a look at Loren's blog post about Cell Arrays and Their Contents