I'm trying to figure out a matrix-oriented way to perform the ismember function by row in MATLAB. That is, if I have matrices
[1 2 3 4 5 6]
[7 8 9 10 11 12]
And I put in
[3 4 5]
[10 11 12]
Into some ismember-ish function, I'd like it to return
[0 0 1 1 1 0]
[0 0 0 1 1 1]
Other than looping over each row of the matrix in a for loop, is there a way to do this?
Assuming that your data are available as matrices A and B
A = [
1 2 3 4 5 6
7 8 9 10 11 12
];
B = [
3 4 5
10 11 12];
you can convert them to cells and then use cellfun
cellA = mat2cell(A, ones(1, size(A,1)), size(A,2));
cellB = mat2cell(B, ones(1, size(B,1)), size(B,2));
membership = cell2mat(cellfun(#ismember, cellA, cellB, 'UniformOutput', false));
This returns
membership =
0 0 1 1 1 0
0 0 0 1 1 1
A = [5 3 4 2]; B = [2 4 4 4 6 8];
[Lia1,Locb1] = ismember(A,B)
Lia1 =
1 1 1 1 0 0
Locb1 =
4 3 3 3 0 0
Related
Is there a function in MATLAB that allows us to do matrix direct sum? For example,
A = [1 2 3
3 4 5]
B = [5 6
7 8
9 8]
and we want the direct sum A ⊕ B that gives us:
directSum(A,B) = [1 2 3 0 0
3 4 5 0 0
0 0 0 5 6
0 0 0 7 8
0 0 0 9 8]
If there is not, what are some quick ways to implement this?
Use blkdiag to compute the direct sum of matrices:
A = [1 2 3; 3 4 5];
B = [5 6; 7 8; 9 8];
blkdiag(A, B)
% ans = 5x5
%
% 1 2 3 0 0
% 3 4 5 0 0
% 0 0 0 5 6
% 0 0 0 7 8
% 0 0 0 9 8
To find the output of the following formula in Matlab, I need to get all elements of a matrix (PDA in the following code) except its first element without using any loop.
Formula(Goal): % EVec = (A11-B11).^2 - (A12-B12).^2 - .. - (Aij-Bij).^2
Ex:
A(:,:,1) = [1 2 3 4; 4 5 6 1];
A(:,:,2) = [0 5 4 3; 2 7 6 0];
A(:,:,3) = [1 2 3 9; 0 6 7 0];
B(:,:,1) = [4 0 3 4; 4 8 0 1];
B(:,:,2) = [0 5 6 1; 0 9 4 3];
B(:,:,3) = [2 0 3 5; 8 6 7 2];
PDA = (A-B).^2;
EVec = PDA(1,1,:) - sum(PDA(?, ?, :)); % The problem is sum(PDA(?, ?,:)).
The result of PDA is:
PDA(:,:,1) =
9 4 0 0
0 9 36 0
% All of them except Val(1,1) = 9.
PDA(:,:,2) =
0 0 4 4
4 4 4 9
% All of them except Val(1,1) = 0.
PDA(:,:,3) =
1 4 0 16
64 0 0 4
% All of them except Val(1,1) = 1.
And my problem is in the output of PDA(1,1,:) - sum(PDA(?, ?, :)) that should be:9-(4+0+0+0+0+9+36+0), 0-(0+4+4+4+4+4+9), 1-(4+0+16+64+0+0+4) = [-40, -29, -87]..Unfortunately it doesn't.
How to get all elements of a matrix except its first element in Matlab?
The first element minus the sum of the remaining elements is just twice the first element minus the sum of all elements, so
>> squeeze(2*PDA(1,1,:) - sum(sum(PDA,1),2))
ans =
-40
-29
-87
Or in newer releases
>> squeeze(2*PDA(1,1,:) - sum(PDA,[1,2]))
ans =
-40
-29
-87
How can i use this recipe for delete row of matrix?
A= [1 2 3 4;5 6 7 8;9 0 1 2;3 4 5 6]
for delete row:
B = A(2,:) = []
I need to delete row and putting up to B.
This is a way for delete row of A without change A:
A = [1 2 3 4;5 6 7 8;9 0 1 2;3 4 5 6]
B = A;
B(2,:) = [];
Result: B = [1 2 3 4;9 0 1 2;3 4 5 6]
I have divied an Image into number of subimages. Now I want to compare the average intensity of each subimage with its 8 neighbors. But in some points there would be less than 8 neighbors. For example for the first block(i=1,J=1), the upperleft block(i-1,j-1) does not exist. How can I check this and skip to the next valid one?
file='myimg.bmp';
I=imread(file);
blockSizeR = 128; % Rows in block.
blockSizeC = 128; % Columns in block.
wholeBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, wholeBlockRows), rem(rows, blockSizeR)];
wholeBlockCols = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, wholeBlockCols), rem(columns, blockSizeC)];
ca = mat2cell(I, blockVectorR, blockVectorC);
%get the mean value of each cell
meanValues = cellfun(#(x) mean(x(:)),ca);
for j=1:size(ca(2))
for i=1:size(ca(1))
currentSlice = ca(i,j);
MeanOfCurrentSlice = cellfun(#(x) mean(x(:)),currentSlice);
%here I want to minus the 8 neighbors average grayscale intensity from the currentSlice average grayscale inensity and take the absolute sum
end
end
A solution that give you the index of the nearest neighbor for each element:
%creation of the index matrix (here a 3x3 matrix)
M = reshape([1:9],3,3);
%subdivide the matrix into 3x3 array
IND = nlfilter(M,[3 3],#(x){x(:)});
%elimination of the value where IND == 0 or IND == index value of the element
for ii = 1:size(M,1)
for jj = 1:size(M,2)
IND{ii,jj}(IND{ii,jj}==0|IND{ii,jj}==sub2ind(size(M),ii,jj)) = [];
end
end
PS: nlfilteris part of the image processing toolbox, but it's easy to create your own similar function.
STEP 1:
M =
1 4 7
2 5 8
3 6 9
STEP 2:
IND =
{
[1,1] =
0 0 0 0 1 2 0 4 5
[2,1] =
0 0 0 1 2 3 4 5 6
[3,1] =
0 0 0 2 3 0 5 6 0
[1,2] =
0 1 2 0 4 5 0 7 8
[2,2] =
1 2 3 4 5 6 7 8 9
[3,2] =
2 3 0 5 6 0 8 9 0
[1,3] =
0 4 5 0 7 8 0 0 0
[2,3] =
4 5 6 7 8 9 0 0 0
[3,3] =
5 6 0 8 9 0 0 0 0
}
STEP 3:
IND =
{
[1,1] = %neighbors of the value M[1,1]
2 4 5
[2,1] =
1 3 4 5 6
[3,1] =
2 5 6
[1,2] =
1 2 5 7 8
[2,2] =
1 2 3 4 6 7 8 9
[3,2] =
2 3 5 8 9
[1,3] =
4 5 8
[2,3] =
4 5 6 7 9
[3,3] =
5 6 8
}
I have two matrix:
A=[1 2 3; 4 5 6; 7 8 9]
C=[0 0 2; 0 0 1; 0 0 8]
I want to keep nonzero values of C and create D. Then, replace the zero elements with A matrix.
So D should be:
D=[1 2 2; 4 5 1; 7 8 8]
I try this code:
A=[1 2 3; 4 5 6; 7 8 9]
C=[0 0 2; 0 0 1; 0 0 8]
T=A(C==0)
R=sparse(T)
K=find(sparse(C))
It didn't work
Use:
D = A;
D(C~=0) = C(C~=0);
drorco's answer is the right way to do it, but there is a one-liner that I couldn't resist:
D = ~C.*A + ~~C.*C;
A=[1 2 3; 4 5 6; 7 8 9];
C=[0 0 2; 0 0 1; 0 0 8];
D=A;
inds=find(C~=0);
D(inds)=C(inds)
D =
1 2 2
4 5 1
7 8 8