Replacing Repeated Values in Each Row of a Matrix with NaN - matlab

I am trying to create a function that would replace repeated values in each row of a matrix with NaNs. My code works except that I can't seem to get it to apply to every row. It applies the code to each row, but will only accept "n" as the number of columns, not the number of rows, so if there are more rows than columns then it won't do every row. For example, if I have 8 rows and 6 columns it will only let me put in 6 for n, not 8. Yet it applies the code to the rows but only the first 6 rows. Here is a working example:
X = [1 2 3 4 4 4; 1 2 3 4 5 5; 1 2 2 2 2 2; 1 2 3 3 3 3;
1 1 1 1 1 1; 1 2 3 4 4 4; 1 2 3 3 3 3; 1 2 2 2 2 2];
n=6
nanfill = nan(1,n);
for i=1:n
temp = [unique(X(i,:)), nanfill];
X(i,:) = temp(1:n);
end
It gives me this for my original matrix:
1 2 3 4 4 4
1 2 3 4 5 5
1 2 2 2 2 2
1 2 3 3 3 3
1 1 1 1 1 1
1 2 3 4 4 4
1 2 3 3 3 3
1 2 2 2 2 2
Then this is the output I get for the new matrix after running my code:
1 2 3 NaN NaN NaN
1 2 3 4 NaN NaN
1 2 NaN NaN NaN NaN
1 2 3 NaN NaN NaN
1 NaN NaN NaN NaN NaN
1 2 3 4 NaN NaN
1 2 3 3 3 3
1 2 2 2 2 2
If I attempt to put in the number of rows, 8, for "n" then it gives me the following error:
Unable to perform assignment because the size of the left side is 1-by-6 and the size
of the right side is 1-by-8.

If the values in each row are always non-decreasing, as in your example, you can use the following. It computes the difference between each entry and the one to its left, and writes NaN if that difference is zero:
X([false(size(x,1),1) ~diff(X,[],2)]) = NaN;

That is because your matrix's rows have elements in them. You can't assign a vector that is 8 elements long into a shorter one. Better would be to check the length of the rows so that they will definitely match:
X = [1 2 3 4 4 4; 1 2 3 4 5 5; 1 2 2 2 2 2; 1 2 3 3 3 3;
1 1 1 1 1 1; 1 2 3 4 4 4; 1 2 3 3 3 3; 1 2 2 2 2 2];
n=size(X,2) % <- # of columns (length of one row)
nanfill = nan(1,n);
for i=1:n
temp = [unique(X(i,:)), nanfill];
X(i,:) = temp(1:n);
end

Related

Cumulative matrix which accounts for column start points

I have a simple example dataset below:
a =
1 1 1 NaN NaN
1 1 1 NaN NaN
1 1 1 1 NaN
1 1 1 1 1
1 1 1 1 1
I want to work out the average cumulative value per row. However, cumsum gives the following output:
cumsum(a)
1 1 1 NaN NaN
2 2 2 NaN NaN
3 3 3 1 NaN
4 4 4 2 1
5 5 5 3 2
Then calculating a row mean gives:
nanmean(a,2)
1
2
2.5
3
4
I want to be able to account for the fact that different columns start later i.e. the row mean values for rows (3:5) are reduced with respect to their true values due to low numbers in columns (4:5).
I want to achieve this by replacing the last NaN above the first numeric element in each column in the matrix (a) with the mean of the other columns in that row in the cumulative matrix.This would need to be done iteratively to reflect the changing values in the cumulative matrix. So the new matrix would first look as follows:
(a)
1 1 1 NaN NaN
1 1 1 *2* NaN
1 1 1 1 NaN
1 1 1 1 1
1 1 1 1 1
which would lead to:
cumsum(a)
1 1 1 NaN NaN
2 2 2 2 NaN
3 3 3 3 NaN
4 4 4 4 1
5 5 5 5 2
and then iteratively, (a) would equal:
(a)
1 1 1 NaN NaN
1 1 1 2 NaN
1 1 1 1 *3*
1 1 1 1 1
1 1 1 1 1
which would lead to:
cumsum(a)
1 1 1 NaN NaN
2 2 2 2 NaN
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
which would give the desired row means values as:
nanmean(a,2)
1
2
3
4
5
There may be a way to further vectorise this. However, I think that because each row depends on the previous values, you have to update the matrix row-by-row as follows:
% Cycle through each row in matrix
for i = 1:length(a)
if i > 1
% This makes elements equal to the sum of themselves and above element
% Equivalent outcome to cumsum
a(i,:) = a(i,:) + a(i-1,:);
end
% Replace all NaN values in the row with the average of the non-NaN values
a(i,isnan(a(i,:))) = mean(a(i,~isnan(a(i,:))));
end
This replicates your input and output examples. It doesn't replicate all your iterative steps, it in fact uses many less steps, only 5 (number of rows) for entire operation.
Edit: equally,
for i = 1:length(a)
% Replace all NaN values in the row with the average of the non-NaN values
a(i,isnan(a(i,:))) = mean(a(i,~isnan(a(i,:))));
end
a = cumsum(a);

Find the rows of a matrix with conditions concerning the values of certain columns in matlab

As the title says, I want to find all rows in a Matlab matrix that in certain columns the values in the row are equal with the values in the previous row, or in general, equal in some row in the matrix. For example I have a matrix
1 2 3 4
1 2 8 10
4 5 7 9
2 3 6 4
1 2 4 7
and I want to find the following rows:
1 2 3 4
1 2 3 10
1 2 4 7
How do I do something like that and how do I do it generally for all the possible pairs in columns 1 and 2, and have equal values in previous rows, that exist in the matrix?
Here's a start to see if we're headed in the right direction:
>> M = [1 2 3 4;
1 2 8 10;
4 5 7 9;
2 3 6 4;
1 2 4 7];
>> N = M; %// copy M into a new matrix so we can modify it
>> idx = ismember(N(:,1:2), N(1,1:2), 'rows')
idx =
1
1
0
0
1
>> N(idx, :)
ans =
1 2 3 4
1 2 8 10
1 2 4 7
Then you can remove those rows from the original matrix and repeat.
>> N = N(~idx,:)
N =
4 5 7 9
2 3 6 4
this will give you the results
data1 =[1 2 3 4
1 2 8 10
4 5 7 9
2 3 6 4
1 2 4 7];
data2 = [1 2 3 4
1 2 3 10
1 2 4 7];
[exists,position] = ismember(data1,data2, 'rows')
where the exists vector tells you wheter the row is on the other matrix and position gives you the position...
a less elegant and simpler version would be
array_data1 = reshape (data1',[],1);
array_data2 = reshape (data2',[],1);
matchmatrix = zeros(size(data2,1),size(data1,1));
for irow1 = 1: size(data2,1)
for irow2 = 1: size(data1,1)
matchmatrix(irow1,irow2) = min(data2(irow1,:) == data1(irow2,:))~= 0;
end
end
the matchmatrix is to read as a connectivity matrix where value of 1 indicates which row of data1 matches with which row of data2

identify the adjacent pixels in matlab

Let's assume A be,
1 1 1 1 1 1
1 2 2 3 3 3
4 4 2 2 3 3
4 4 2 2 2 3
4 4 4 4 3 3
5 5 5 5 5 5
I need to identify all the numbers which are adjacent to a particular intensity value. E.g. the intensities 1, 3, and 4 are adjacent to the intensity value 2.
What is the effective way to do it in Matlab?
I can use the following,
glcm = graycomatrix(A)
But if A have a larger number of intensity values e.g. 10000 graycomatrix will not be an efficient method.
You can build a mask with a 2D convolution, select the values according to that mask, and then reduce them to unique values:
% // Data:
A = [ 1 1 1 1 1 1
1 2 2 3 3 3
4 4 2 2 3 3
4 4 2 2 2 3
4 4 4 4 3 3
5 5 5 5 5 5 ];
value = 2;
adj = [0 1 0; 1 0 1; 0 1 0]; %// define adjacency. [1 1 1;1 0 1;1 1 1] to include diagonals
%// Let's go
mask = conv2(double(A==value), adj, 'same')>0; %// pixels adjacent to those equal to `value`
result = unique(A(mask));
In the example, this produces
result =
1
2
3
4
Note that the result includes 2 because some pixels with value 2 have adjacent pixels with that value.

Creating an index matrix depending on Reference matrix and matrix of Data matlab

given matrix A of size 6 by 6 contain blocks of numbers,each block of size 2 by 2, and outher reference matrix R of size 2 by 12 also contain blocks of numbers, each block of size 2 by 2. the perpse of the whole process is to form a new matrix, called the Index matrix, contain index's that refer to the position of the blocks within the matrix A based on the order of the blocks within the reference matrix R. and here is an exemple
matrix A:
A =[1 1 2 2 3 3;
1 1 2 2 3 3;
1 1 3 3 4 4;
1 1 3 3 4 4;
4 4 5 5 6 6;
4 4 5 5 6 6 ]
matrix R:
R=[1 1 2 2 3 3 4 4 5 5 6 6;
1 1 2 2 3 3 4 4 5 5 6 6 ]
the new matrix is:
Index =[1 2 3;
1 3 4;
4 5 6]
any ideas ?
With my favourite three guys - bsxfun, permute, reshape for an efficient and generic solution -
blksz = 2; %// blocksize
num_rowblksA = size(A,1)/blksz; %// number of blocks along rows in A
%// Create blksz x blksz sized blocks for A and B
A1 = reshape(permute(reshape(A,blksz,num_rowblksA,[]),[1 3 2]),blksz^2,[])
R1 = reshape(R,blksz^2,1,[])
%// Find the matches with "bsxfun(#eq" and corresponding indices
[valid,idx] = max(all(bsxfun(#eq,A1,R1),1),[],3)
%// Or with PDIST2:
%// [valid,idx] = max(pdist2(A1.',reshape(R,blksz^2,[]).')==0,[],2)
idx(~valid) = 0
%// Reshape the indices to the shapes of blocked shapes in A
Index = reshape(idx,[],num_rowblksA).'
Sample run with more random inputs -
>> A
A =
2 1 1 2
1 2 2 1
1 1 1 1
2 2 2 2
1 2 2 1
1 2 1 1
>> R
R =
2 1 1 1 1 2 2 2 1 1 1 1
2 1 2 1 1 2 2 1 2 2 2 1
>> Index
Index =
0 0
5 5
3 0

Summation of matrices

I want to sum together each cell in the same position for each matrix. I have k amount of (i,j) matrices stored in MATLAB as (i,j,k) and I want to create one matrix which is the sum of all them - however the MATLAB command sums together every value in each column whereas I want to sum together each cell in the same position from each matrix.
1 3 4 3 4 0 2 4 4
0 3 1 2 7 8 0 3 1
9 0 2 0 1 2 1 2 3
I want to create a matrix that is:
1+3+2 3+4+4 4+0+4
0+2+1 3+7+3 1+8+1
9+0+1 0+1+2 2+2+3
=
6 11 8
3 13 10
10 3 7
Use a second input to sum specifying the dimension along which to sum (in your case, 3):
>> A(:,:,1) = [ 1 3 4
0 3 1
9 0 2 ];
>> A(:,:,2) = [ 3 4 0
2 7 8
0 1 2 ];
>> A(:,:,3) = [ 2 4 4
0 3 1
1 2 3 ];
>> sum(A,3)
ans =
6 11 8
2 13 10
10 3 7