Replace Nan by a number in a cell array with Matlab - matlab

a = { [5 5 5 6 ] [ Nan Nan Nan Nan]; [ 7 8 8 8 ] [1 2 3 5] }
i want to replace a{1,2} by [1 1 1 1 ]
Is it possible? because I tried this function:
function k = non(a)
[l ,c ] = size(a);
for i= 1 : l
for j =1 : c
if a{i,j} == [ NaN NaN NaN NaN ]
k{i,j} = [ 1 1 1 1]
else
k{i,j} = a{i,j} ;
end
end
end
end
but it doesn't work.

You can define the following simple function:
function matrix = replace_nan(matrix, value)
matrix(isnan(matrix)) = value;
end
and then use cellfun to execute it over all elements of your cell array (I have generalized your problem slightly by allowing you to define value as the number to replace the NaN with, and making the elements variable length):
>> a = {[ 3 NaN] [NaN NaN NaN] [1 2 3 4 5 NaN 0 NaN]};
>> value = 1; %% the value to replace the NaN with
>> z = cellfun(#replace_nan, a, repmat( {value}, size(a,1), size(a,2)) , 'UniformOutput', 0);
>> z{1}
ans =
3 1
>> z{2}
ans =
1 1 1
>> z{3}
ans =
1 2 3 4 5 1 0 1
A few comments on the use of cellfun here: cellfun takes a function, in this case replace_nan, and a cell array, in this case a, and iterates the function call to replace_nan(). If replace_nan were a single argument function, we could say cellfun(#replace_nan, a), but the way I defined it, the function takes two arguments. The way to specify that in cellfun is to provide a second cell array with the value arguments. That is the repmat({1},size(a,1),size(a,2)) which creates a second cell array with the same dimensions as a, but filled with 1's.
EDIT: The comment discussion brings up a good point: you cannot use == to test for NaN, you must use MATLAB's isnan() function.
>> [NaN NaN] == [NaN NaN]
ans =
0 0
>> isnan( [NaN NaN] )
ans =
1 1

Or even shorter this way:
a(cellfun(#(cell) any(isnan(cell(:))),a))={[1 1 1 1]};

Related

Octave: A(~isnan(A)) results vector containing NaNs?

May be I don't get a basic thing, but I recently discovered this behaviour, and by now I don't understand what's happening:
A = [3 NaN .12 NaN NaN 9]
A =
3 NaN 0.12 NaN NaN 9
>> nansA = isnan(A)
nansA =
0 1 0 1 1 0
>> nnansA = ~isnan(A)
nnansA =
1 0 1 0 0 1
>> nnansA1 = ~isnan(A(1:end))
nnansA1 =
1 0 1 0 0 1
>> nnansA2 = ~isnan(A(2:end))
nnansA2 =
0 1 0 0 1
>> AnnansA1 = A(~isnan(A(1:end)))
AnnansA1 =
3 0.12 9
>> **AnnansA2 = A(~isnan(A(2:end)))
AnnansA2 =
NaN NaN
What is happening here?
Does this happen in Matlab too?
I would expect something like ... AnnansA2 = 0.12 9
What is happening here is that you're indexing A with a logical array of a different size and expect the indexing to not start at the beginning.
Let's deconstruct, from the inside:
>> A = [3 NaN .12 NaN NaN 9]
A =
3.0000 NaN 0.1200 NaN NaN 9.0000
>> # B a new array, with 5 elements (A had 6 elements)
>> B = A(2:end)
B =
NaN 0.1200 NaN NaN 9.0000
>> # mask is a logical array with 5 elements, like B, and not 6, like A.
>> # mask knows nothing about A or B. It simply "selects" (indexes) the
>> # 1st, 3rd, and 4th element of any array.
>> mask = isnan (B)
mask =
1 0 1 1 0
>> # Invert the mask, now "selects" the 2nd and 5th element of any array.
>> not_mask = ! mask
not_mask =
0 1 0 0 1
>> # Return the 2nd and 5th element of A.
>> A(not_mask)
ans =
NaN NaN
I think you're surprised by the behaviour because you expect that A(2:end) "remembers" that it comes from A to index the right "region" of A. This does not happen, it's just a logical array that "remembers" nothing from which array it came (and often used to index different arrays).
As a side note, and answering one of your questions, Matlab behaviour is the same as Octave.
Anyway, what you're doing looks a bit odd, maybe do this instead:
A(! isnan (A))(2:end)
You're off by one.
You need to do AnnansA2 = A(~isnan(A(1:end)))
If you want to return only the last two non-nans, index the result like;
ananIdxs = ~isnan(A)
AnnansA2 = A(ananIdxs(2:end))

Replace non-NaN values with their row indices within matrix

I have the 4x2 matrix A:
A = [2 NaN 5 8; 14 NaN 23 NaN]';
I want to replace the non-NaN values with their associated indices within each column in A. The output looks like this:
out = [1 NaN 3 4; 1 NaN 3 NaN]';
I know how to do it for each column manually, but I would like an automatic solution, as I have much larger matrices to handle. Anyone has any idea?
out = bsxfun(#times, A-A+1, (1:size(A,1)).');
How it works:
A-A+1 replaces actual numbers in A by 1, and keeps NaN as NaN
(1:size(A,1)).' is a column vector of row indices
bsxfun(#times, ...) multiplies both of the above with singleton expansion.
As pointed out by #thewaywewalk, in Matlab R2016 onwards bsxfun(#times...) can be replaced by .*, as singleton expansion is enabled by default:
out = (A-A+1) .* (1:size(A,1)).';
An alternative suggested by #Dev-Il is
out = bsxfun(#plus, A*0, (1:size(A,1)).');
This works because multiplying by 0 replaces actual numbers by 0, and keeps NaN as is.
Applying ind2sub to a mask created with isnan will do.
mask = find(~isnan(A));
[rows,~] = ind2sub(size(A),mask)
A(mask) = rows;
Note that the second output of ind2sub needs to be requested (but neglected with ~) as well [rows,~] to indicate you want the output for a 2D-matrix.
A =
1 1
NaN NaN
3 3
4 NaN
A.' =
1 NaN 3 4
1 NaN 3 NaN
Also be careful the with the two different transpose operators ' and .'.
Alternative
[n,m] = size(A);
B = ndgrid(1:n,1:m);
B(isnan(A)) = NaN;
or even (with a little inspiration by Luis Mendo)
[n,m] = size(A);
B = A-A + ndgrid(1:n,1:m)
or in one line
B = A-A + ndgrid(1:size(A,1),1:size(A,2))
This can be done using repmat and isnan as follows:
A = [ 2 NaN 5 8;
14 NaN 23 NaN];
out=repmat([1:size(A,2)],size(A,1),1); % out contains indexes of all the values
out(isnan(A))= NaN % Replacing the indexes where NaN exists with NaN
Output:
1 NaN 3 4
1 NaN 3 NaN
You can take the transpose if you want.
I'm adding another answer for a couple of reasons:
Because overkill (*ahem* kron *ahem*) is fun.
To demonstrate that A*0 does the same as A-A.
A = [2 NaN 5 8; 14 NaN 23 NaN].';
out = A*0 + kron((1:size(A,1)).', ones(1,size(A,2)))
out =
1 1
NaN NaN
3 3
4 NaN

how to join N vectors into matrix in matlab?

I tried looking this up, but don't really know what to look for...
I need to "table-join" N vectors, meaning,
to create a matrix in which every input vector has a row, and every possible entry has a column.
Also it would be nice to have a translation vector to has easy access to which column is responsible for which entry
for example
a = [3, 2, 4, 9]
b = [3, 1, 5, 9]
c = [2, 4, 9, 6]
then
join(a, b, c) =
[
3; 2; nan; 4; nan; 9; nan,
3; nan; 1; nan; 5; 9; nan,
nan; 2; nan; 4; nan; 9; 6,
]
with a translation vector
[3,2,1,4,5,9,6]
so if I find out something about the i'th column, I can easily know what that column represents.
I prefer the join operation to be able to take in n vectors (they can be of the same length), but 2 is also ok.
Also, on a second glance, this data representation seems a bit redundant at some points. Perhaps there is a better way to even represent the "join-matrix"
Thanks
Basically you want to construct your translation vector using all the possible unique inputs in the order that they were received. To do this, we can concatenate all of the inputs together than find the unique values.
values = cat(1, [3, 2, 4, 9], [3, 1, 5, 9], [2, 4, 9, 6])
%// 3 2 4 9
%// 3 1 5 9
%// 2 4 9 6
translationVector = unique(values, 'stable')
%// 3 2 1 4 5 9 6
Then we want to use ismember to return a logical array for any given input to specify which values of our translation vector are present in the input argument.
columns = ismember(translationVector, [3 2 4 9])
%// 1 1 0 1 0 1 0
We then want to set just those columns in the output matrix.
output(1, columns) = [3 2 4 9];
%// 3 2 NaN 4 NaN 9 NaN
%// NaN NaN NaN NaN NaN NaN NaN
%// NaN NaN NaN NaN NaN NaN NaN
We then repeat this for all of your input arrays.
Implementation
Here is some code that accomplishes that.
function [out, translationVector] = yourjoin(varargin)
%// Make sure all inputs are row vectors
varargin = cellfun(#(x)x(:).', varargin, 'uni', 0); %'
%// compute the translation vector
translationVector = unique(cat(1, varargin{:}), 'stable');
%// Pre-allocate your matrix of NaNs
out = nan(numel(varargin), numel(translationVector));
%// Fill in each row using each input argument
for k = 1:numel(varargin)
%// Identify columns that we have
toreplace = ismember(translationVector, varargin{k});
%// Set the values of those columns to the input values
out(k,toreplace) = varargin{k};
end
end
And then as a test:
a = [3 2 4 9];
b = [3 1 5 9];
c = [2 4 9 6];
D = yourjoin(a,b,c)
3 2 NaN 4 NaN 9 NaN
3 NaN 1 NaN 5 9 NaN
NaN 2 NaN 4 NaN 9 6

Number of Non-NaN Rows in a 3D Matrix MATLAB

I have a 3D matrix with NaN padded to obtain equal number of rows in each 2D matrix i.e each (:,:,ind). Now i need to find the number of actual non-NaN rows in each (:,:,ind).
A simple example of what I need:
% Input:
A(:,:,1) = [ 1 1;
2 2;
NaN NaN];
A(:,:,2) = [ 2 2;
NaN NaN;
NaN NaN];
% Function call:
B = callingfunction(A);
% Output:
B = [2 1] % Number of Non-NaN rows in each 2D Matrix
Approach #1
B = squeeze(sum(all(~isnan(A),2),1))
Here's the build-up process to get a hang of it -
Start>>> Given A:
>> A
A(:,:,1) =
1 1
2 2
NaN NaN
A(:,:,2) =
2 2
NaN NaN
NaN NaN
1) Detect all non-NaN positions:
>> ~isnan(A)
ans(:,:,1) =
1 1
1 1
0 0
ans(:,:,2) =
1 1
0 0
0 0
2) Find rows with all non-Nan elements:
>> all(~isnan(A),2)
ans(:,:,1) =
1
1
0
ans(:,:,2) =
1
0
0
3) Sum up the number of all such rows:
>> sum(all(~isnan(A),2),1)
ans(:,:,1) =
2
ans(:,:,2) =
1
4) Get the result as a 1D array:
>> squeeze(sum(all(~isnan(A),2),1))
ans =
2
1
Approach #2
B = squeeze(sum(~any(isnan(A),2),1))
Use the same break-up-my-code-into-pieces process as listed earlier here and in all your future MATLAB codes and all past MATLAB codes that didn't make sense to do so now!

Removing NaN elements from a matrix

There is one NaN element per row, I want to remove it.
A=[NaN 1 2;
3 NaN 4;
NaN 5 6];
The desired output is:
[1 2;
3 4;
5 6]
A = [NaN 1 2 ; 3 NaN 4; NaN 5 6]
sz = size(A);
B = reshape(A', size(A,1)*size(A,2), 1);
B(isnan(B)) = [];
B = reshape(B, sz(2)-1, sz(1))'
I thought it could be done in one line, but I was wrong. See solution below:
Given (added row helps me debug my indexing below):
>> A = [NaN 1 2 ; 3 NaN 4; NaN 5 6; 7 8 NaN]
A =
NaN 1 2
3 NaN 4
NaN 5 6
7 8 NaN
Then:
>> Atrans = A';
>> B = reshape( Atrans(~isnan(Atrans)) ,[],size(Atrans,2))'
B =
1 2
3 4
5 6
7 8
Incidentally, the Matlab idiom of performing a simple logical check on an array within an logical indexing operation is very common and incredibly useful. The archetypical example is:
>> x(x>0) %This returns a 1D column vector of all values of x
%which are greater than 0, regardless of the initial
%size of x. Multidimensional inputs are unwrapped
%column-first
Everything else above is size and dimension handling.
Here it is - please note that the code is not robust. It assumes that indeed in every row there is a NaN element.
While it is not a vectorized solution, it has other advantages - like a clear code.
for i=1:size(A,1)
x = A(i,:);
x(isnan(x)) = [];
B(i,:) = x;
end
B
B =
1 2
3 4
5 6