matlab - cellfun sum all elements of each cell - matlab

Hi I have populated a cell array using:
D(i) = {dist};
D = reshape(D, w, h)
so that if i have:
pix1 = D{1,1};
pix2 = D{2,2};
I get
pix1 =
1 2 3
pix2 =
4 5 6
What I want to do is sum all the elements in each pix, and then take those results and form a matrix i.e.
sum(pix1) = 6
sum(pix2) = 15
matrix =
6 15
where in this case matrix is a 1X2 matrix (mine is a lot larger).
I am trying to do this using:
field = cellfun(#(dist) sum(dist(:)), D,'UniformOutput', false);
but this just gives me a matrix full of NaN's. Where am I going wrong?

A = {[1 2 4], [4 5 6]};
B = cellfun(#sum, A)
results in
B = [6 15]
B = [7 15]

In case you have NaNs in your cells and you wish to ignore them you may use nansum:
A = {[1, 2, NaN], [3, NaN, 4, 5]; [6, NaN], [10, -3, NaN, 4]};
B = cellfun( #nansum, A )
Results with
B =
3 12
6 11

Related

Octave: Access value from a matrix by passing indices in a vector?

I need to access/edit values in an n dimensional matrix M, by passing a vector V, containing indices.
let
M = [ 1,2,3;
4,5,6;
7,8,9;];
index vectors be
V1=[2,1];
V2=[1,2];
now, M(V1) should give 4
and M(V2) should give 2;
problem is n is not fixed and I don't want do looping to access values like M(idx_1,idx_2,...idx_n)
Many functions handle N dimensions by accepting a matrix with N columns. However, many others require you to pass N arguments instead. That is the case of sub2ind (convert subscript indices into linear indices) which you need to solve your specific problem:
octave> M = randi (9, 3)
M =
2 9 4
5 9 5
2 6 4
octave> ind = sub2ind (size (M), [2 1], [1 2]) # sub2ind (dims, rows, cols, 3rd_dim, 4th_dim, ...)
ind =
2 4
octave> M(ind)
ans =
5 9
The main trick to handle N dimensions is understanding comma separated lists (cs-lists) and how you can get them from cell arrays. Using a 2D example like yours (but ignoring your v1 and v2 nonsense --- you should use a matrix v where each row is a point and each columns is a dimension), you can do:
octave> v = [2 1; 1 2];
octave> ind = sub2ind (size (M), num2cell (v, 1){:})
ind =
2
4
octave> M(ind)
ans =
5
9
The only thing that should need to be explained in the example above is:
octave> num2cell (v, 1) # returns a cell array
ans =
{
[1,1] =
2
1
[1,2] =
1
2
}
octave> num2cell (v, 1){:} # transform the cell array into a cs-list
ans =
2
1
ans =
1
2
And you can use this to truly handle N dimensions:
octave> M = rand (9, 5, 5, 5, 5, 5);
octave> v = [5 5 5 5 5; 1 2 3 4 5; 3 2 4 4 1];
octave> ind = sub2ind (size (M), num2cell (v, 1){:});
octave> M(ind)
ans =
0.13726
0.14020
0.78660
octave> [M(5, 5, 5, 5, 5); M(1, 2, 3, 4, 5); M(3, 2, 4, 4, 1)]
ans =
0.13726
0.14020
0.78660

How to expand my cell without losing information

I have this a cell that holds 4 vectors, and I'd like to expand it so it could hold another vector. Thanks in advance.
a = cell(4, 1);
a{1} = [1, 2, 3];
a{2} = [1, 4, 9];
a{3} = [1, 4, 9];
a{4} = [1; 5];
And I would like to add a new vector [2, 7] so that I will get this
a{1} = [2, 7];
a{2} = [1, 2, 3];
a{3} = [1, 4, 9];
a{4} = [1, 4, 9];
a{5} = [1; 5];
How could I do that ?
a = cell(4, 1);
a{1} = [1, 2, 3];
a{2} = [1, 4, 9];
a{3} = [1, 4, 9];
a{4} = [1; 5];
a = [[2, 7];a]
Yielding:
a =
[1x2 double]
[1x3 double]
[1x3 double]
[1x3 double]
[2x1 double]
The name for what you are trying to do is concatenation. In MATLAB, square brackets are an implicit method of concatenation. While you typically think about these being used to concatenate numbers to form a vector or matrix:
x = [1, 2, 3, 4] % 1 x 4
You can also use them to concatenate other data types (including cell arrays)
y = [{1,2}, 5, {4, 5}]; % 1 x 3 Cell Array
It is also important to keep track of the dimension of concatenation. With square brackets you can either concatenate across the columns (2nd dimension) using a comma or across the rows (1st dimension using a semi-colon).
size([1,2,3])
1 x 3
size([1;2;3])
3 x 1
While square brackets are a quick and easy solution, I prefer to be a little more explicit. For this, MATLAB has the following functions: cat, horzcat, and vertcat. I personally prefer cat since you can specify concatenation in any arbitrary dimension.
x = cat(2, 100, 200, 300); % 1 x 3
100 200 300
x = cat(1, 100, 200, 300); % 3 x 1
100
200
300
x = cat(3, 100, 200, 300) % 1 x 1 x 3
x(:,:,1) =
100
x(:,:,2) =
200
x(:,:,3) =
300
So back to your original question, you want to concatenate in the 1st dimension (rows) so you could simply do the following:
a = cell(4, 1);
a{1} = [1, 2, 3];
a{2} = [1, 4, 9];
a{3} = [1, 4, 9];
a{4} = [1; 5];
a = cat(1, [2, 7], a);
Also remember that this form of using concat or the square brackets can be applied to most datatypes within MATLAB including structs among others.

Find the minimum positive difference between elements in vector

A = [1 3 5 8]
B = [1 2 3 4 5 6 7 8]
I would like to create a vector C which returns the rownumber of the element in vector A with the smallest non-negative difference to each element in vector B.
So, given the example above, it should return:
C = [1 2 2 3 3 4 4 4]
I'm sure there are many ways to do this. Here's one:
A = [1 3 5 8]
B = [1 2 3 4 5 6 7 8]
%create matrices of the values to subtract
[a,b] = meshgrid(A,B);
%subtract
aLessB = a-b;
%make sure we don't use the negative values
aLessB(aLessB < 0) = Inf;
%sort the subtracted matrix
[dum, idx] = sort(aLessB, 2, 'ascend');
idx(:,1) is the solution you are looking for.
An alternative solution:
D = bsxfun(#minus, A', B);
D(D < 0) = Inf;
[~, C] = min(D, [], 1);

Changing multiple elements (of known coordinates) of a matrix without a for loop

I have a matrix say
Z = [1 2 3;
4 5 6;
7 8 9]
I have to change its values, say at positions (2,2) and (3,1), to some specified value. I have two matrices rowNos and colNos which contain these positions:
rowNos = [2, 3]
colNos = [2, 1]
Let's say I want to change the value of elements at these positions to 0.
How can I do it without using for loop?
Use sub2ind, it'll convert your sub-indices to linear indices, which is a number pointing at one exact spot in the matrix (more info).
Z = [ 1 2 3 ; 4 5 6 ; 7 8 9];
rowNos = [2, 3];
colNos = [2, 1];
lin_idcs = sub2ind(size(Z), rowNos, colNos)
If you want to operate on all elements on a specific row and column (elements in higher dimensions that is), you can also address them using linear indexing. It only becomes a bit trickier of calculating them:
Z = reshape(1:4*4*3,[4 4 3]);
rowNos = [2, 3];
colNos = [2, 1];
siz = size(Z);
lin_idcs = sub2ind(siz, rowNos, colNos,ones(size(rowNos))); % just the first element of the remaining dimensions
lin_idcs_all = bsxfun(#plus,lin_idcs',prod(siz(1:2))*(0:prod(siz(3:end))-1)); % all of them
lin_idcs_all = lin_idcs_all(:);
Z(lin_idcs_all) = 0;
experiment a bit with sub2ind, and go through my code step-by-step to understand it.
It would've been easier if it was the first dimension you wanted to take all elements off, then you could have used the colon operator :
Z = reshape(1:3*4*4,[3 4 4]);
rowNos = [2, 3];
colNos = [2, 1];
siz = size(Z);
lin_idcs = sub2ind(siz(2:end),rowNos,colNos);
Z(:,lin_idcs) = 0;
Use sub2ind with multiple entries for rows and columns
Z(sub2ind(size(Z), rowNos, colNos))=0
Example:
Z = [1 2 3;
4 5 6;
7 8 9];
rowNos = [2, 3];
colNos = [2, 1];
Z(sub2ind(size(Z), rowNos, colNos))=0
Z =
1 2 3
4 0 6
0 8 9
You would like to do this
z(rowNos, colNos)
but you can not - MATLAB does a Cartesian product of the indices. You can do this trick
idx=(colNos-1)*size(z, 1)+rowNos;
z(idx)=0
Flatten the z-matrix and access it through a linear index, which is a combination of rowNos and colNos. Remember that MATLAB flattens the matrix by columns (column-based matrix storage).

MatLab - histc with many edges vector

Consider this :
a = [1 ; 7 ; 13];
edges = [1, 3, 6, 9, 12, 15];
[~, bins] = histc(a, edges)
bins =
1
3
5
Now I would like to have the same output, but with a different "edges" vector for each a value, i.e. a matrix instead of a vector for edges. Exemple :
a = [1 ; 7 ; 13];
edges = [ 1, 3, 6 ; 1, 4, 15 ; 1, 20, 30];
edges =
1 3 6
1 4 15
1 20 30
indexes = theFunctionINeed(a, edges);
indexes =
1 % 1 inside [1, 3, 6]
2 % 7 indide [1, 4, 15]
1 %13 inside [1, 20, 30]
I could do this with histc inside a for loop, by I'm trying to avoid loops.
If you transform your arrays to cell arrays, you can try
a = {1 ; 7 ; 13};
edges = {[ 1, 3, 6 ];[ 1, 4, 15] ; [1, 20, 30]};
[~, indexes] = cellfun(#histc, a, edges,'uniformoutput', false)
This results in
indexes =
[1]
[2]
[1]
~edit~
To transform your matrices into cell arrays you can use num2cell:
a = num2cell(a);
edges = num2cell(edges, 2);
You could also do:
a = [1; 7; 13];
edges = [1 3 6; 1 4 15; 1 20 30];
bins = sum(bsxfun(#ge, a, edges), 2)
The result:
>> bins
bins =
1
2
1