how to remove the column with all the same elements in matlab? - matlab

Suppose the input is:
[1 2 3;
2 3 3;
3 4 3;
3 5 3;]
The expected output would be:
[1 2;
2 3;
3 4;
3 5;]
The reason to remove the third column is because all the elements in the third column is the same. Is there a default matlab function for this?

A(:,sum(abs(diff(A)))>0,1)
"Keep the columns where the difference is larger than zero"

Both the posted answers are incorrect. Test the edge cases where A only has 1 or 2 rows:
i.e:
A = [1 2 3];
or:
A = [1 2 3;
2 3 3];
diff and any need to be supplied with the correct dimension:
A = A(:,any(diff(A,1,1),1));
This outputs:
A = [1 2 3; 2 3 3];
EDU>> A(:,any(diff(A,1,1),1))
ans =
1 2
2 3
and
A = [1 2 3]
EDU>> A(:,any(diff(A,1,1),1))
ans =
Empty matrix: 1-by-0
Also, IMO this, semantically, makes the most sense:
A(:,all(bsxfun(#eq,A,A(1,:)),1)) = []

How about:
A =
1 2 3
2 3 3
3 4 3
3 5 3
B = A==repmat(A(1,:),size(A,1),1)
B =
1 1 1
0 0 1
0 0 1
0 0 1
C = sum(B) == size(A,1)
C =
0 0 1
A(:,C) =[]
A =
1 2
2 3
3 4
3 5
In one line:
A(:, sum(A==repmat(A(1,:),size(A,1),1)) == size(A,1)) = []

Related

Indexing a matrix in matlab according to conditions set on other matrices

I am trying to index my matrix based on two conditions, I'll explain.
Let's say I have two matrices:
a = [7 3 4; 5 6 7; 4 8 0];
b = [1 9 8; 2 4 6; 6 1 6];
And a third matrix to index:
c = [1 2 3; 4 5 6; 7 8 9];
My aim is to index c in a way that I get a 3x3 matrix in which only the values of c are copied over for whose indexes the following conditions are met and the rest are zeros.
a <= 5, b >= 6
Resulting matrix:
result = [0 2 3; 0 0 0; 7 0 9]
I hope I was able to explain my problem.
Given
a = [7 3 4; 5 6 7; 4 8 0];
b = [1 9 8; 2 4 6; 6 1 6];
c = [1 2 3; 4 5 6; 7 8 9];
result = zeros(size(c);
Using logical indexing,
>> d = (a <= 5) & (b >= 6)
d =
0 1 1
0 0 0
1 0 1
>> result(d) = c(d)
result =
0 2 3
0 0 0
7 0 9
Loop throw rows and columns and set the result.
for row=1:size(a,1)
for col=1:size(a,2)
if(a(row,col)> b(row,col))
result(row,col) = 0
else
result(row,col) = c(row,col)
end
end
end

Matlab vector to matrix conversion

I want to convert the following vector A into matrix B, best demonstrated by this example:
n = 4;
A = [1 2 3 4 5 6];
B = [ 1 2 3 4;
2 3 4 5;
3 4 5 6; ]
I am currently using a loop to achieve this and wondered if it was possible to vectorize it?
Thanks L.
You can use bsxfun -
A(bsxfun(#plus,[0:numel(A)-n]',1:n))
You can also use hankel -
hankel(A(1:n),A(n:end)).'
Sample run -
>> A = [3,4,6,0,1,2]
A =
3 4 6 0 1 2
>> n
n =
4
>> A(bsxfun(#plus,[0:numel(A)-n]',1:n))
ans =
3 4 6 0
4 6 0 1
6 0 1 2
>> hankel(A(1:n),A(n:end)).'
ans =
3 4 6 0
4 6 0 1
6 0 1 2
If you have the Signal Processing Toolbox you can also use convmtx:
n = 4;
A = [1 2 3 4 5 6];
m = numel(A)-n;
B = flipud(convmtx(A,m+1));
B = B(:,m+1:end-m);

Find the count of elements in one matrix equal to another

I need to compare the elements of two matrices and return a count of how many rows are exactly same. The ismember function returns one column for each column present in the matrix. But I want just one column indicating whether the row was same or not. Any ideas will be greatly appreciated.
If you want to compare corresponding rows of the two matrices, just use
result = all(A==B, 2);
Example:
>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> B = [1 2; 3 0; 5 6]
B =
1 2
3 0
5 6
>> result = all(A==B, 2)
result =
1
0
1
If you want to compare all pairs of rows:
result = pdist2(A,B)==0;
Example:
>> A = [1 2; 3 4; 1 2]
A =
1 2
3 4
1 2
>> B = [1 2; 3 0]
B =
1 2
3 0
>> result = pdist2(A,B)==0
result =
1 0
0 0
1 0

How to find one value from a matrix

0I have on matrix-
A=[1 2 2 3 5 5;
1 5 5 8 8 7;
2 9 9 3 3 5];
From matrix i need to count now many nonzero elements ,how any 1,how many 2 and how many 3 in each row of given matrix"A".For these i have written one code like:
[Ar Ac]=size(A);
for j=1:Ar
for k=1:Ac
count(:,j)=nnz(A(j,:));
d(:,j)=sum(A(j,:)== 1);
e(:,j)=sum(A(j,:)==2);
f(:,j)=sum(A(j,:)==3);
end
end
but i need to write these using on loop i.e. here i manually use sum(A(j,:)== 1),sum(A(j,:)== 2) and sum(A(j,:)== 3) but is there any option where i can only write sum(A(j,:)== 1:3) and store all the values in the different row i.e, the result will be like-
b=[1 2 1;
1 0 0;
0 1 2];
Matlab experts need your valuable suggestions
Sounds like you're looking for a histogram count:
U = unique(A);
counts = histc(A', U)';
b = counts(:, ismember(U, [1 2 3]));
Example
%// Input matrix and vector of values to count
A = [1 2 2 3 5 5; 1 5 5 8 8 7; 2 9 9 3 3 5];
vals = [1 2 3];
%// Count values
U = unique(A);
counts = histc(A', U)';
b = counts(:, ismember(U, vals));
The result is:
b =
1 2 1
1 0 0
0 1 2
Generalizing the sought values, as required by asker:
values = [ 1 2 3 ]; % or whichever values are sought
B = squeeze(sum(bsxfun(#(x,y) sum(x==y,2), A, shiftdim(values,-1)),2));
Here is a simple and general way. Just change n to however high you want to count. n=max(A(:)) is probably a good general value.
result = [];
n = 3;
for col= 1:n
result = [result, sum(A==col, 2)];
end
result
e.g. for n = 10
result =
1 2 1 0 2 0 0 0 0 0
1 0 0 0 2 0 1 2 0 0
0 1 2 0 1 0 0 0 2 0
Why not use this?
B=[];
for x=1:size(A,1)
B=[B;sum(A(x,:)==1),sum(A(x,:)==2),sum(A(x,:)==3)];
end
I'd do this way:
B = [arrayfun(#(i) find(A(i,:) == 1) , 1:3 , 'UniformOutput', false)',arrayfun(#(i) find(A(i,:) == 2) , 1:3 , 'UniformOutput', false)',arrayfun(#(i) find(A(i,:) == 3) , 1:3 , 'UniformOutput', false)'];
res = cellfun(#numel, B);
Here is a compact one:
sum(bsxfun(#eq, permute(A, [1 3 2]), 1:3),3)
You can replace 1:3 with any array.
you can make an anonymous function for it
rowcnt = #(M, R) sum(bsxfun(#eq, permute(M, [1 3 2]), R),3);
then running it on your data returns
>> rowcnt(A,1:3)
ans =
1 2 1
1 0 0
0 1 2
and for more generalized case
>> rowcnt(A,[1 2 5 8])
ans =
1 2 2 0
1 0 2 2
0 1 1 0

Matlab matrix sort

I 've a specific problem of sorting rows in matlab.
This is mine example entry matrix:
A =
[0 1 1;
0 1 2;
1 0 3;
1 0 4;
1 1 5;
0 1 6;]
and this is "sorting vector"
V=
1
4
6
2
3
5
How to get an output matrix like this:
B=
[0 1 1;
1 0 4;
0 1 6;
0 1 2;
1 0 3;
1 1 5]
?
First I've added vector V to matrix A (last column) but the next step I don't know how it should look. I'm stuck.
In advance, thanks for your time and help :)
To rearrange or select any desired rows:
B = A(V,:);
The same concept could be used for columns and for rearranging, selecting or repeating any desired row or column:
V2 = [3 1 3];
B2 = A(:,V2);
B2 =
1 0 1
2 0 2
3 1 3
4 1 4
5 1 5
6 0 6
Learn about colon operator(:) here:
http://www.mathworks.com/help/matlab/ref/colon.html
This might be the answer:
B = A(V(:),:);