Remove zeros from matrix in matlab - matlab

I have a question and I hope it is not duplicate.
First, I have let say the following matrix:
A=[2 2 2 0 0
1 2 3 0 0
4 5 7 2 0]
I want to remove the zeros from A and return:
A=[2 2 2
1 2 3
4 5 7]
When I do
A(A==0)=[]
I get
A=[2 2 2 1 2 3 4 5 7]
Second, if instead of zeros I want to remove the elements that are greater than something. For example if I want to remove all elements greater than 6 (>6) of the following matrix B:
B=[2 2 2 5 3
1 2 3 6 8
4 5 7 2 1]
I get
A=[2 2 5
1 2 6
4 5 2]
P.S. I know how to do it using loops.

First problem solution
A(:,find(all(A,1)))
Second problem solution
B(:,~any(B>6,1))

Related

Nested loop and conditional statement (Matlab)

If you have a random matrix, for example a 5x5:
A(i,j) = (5 4 3 2 1
4 3 2 1 0
5 4 3 2 1
4 3 2 1 0
5 4 3 2 1)
And a second array:
B(1,j) = (4 5 6 7 8)
How can I then assign values of B to A if this only needs to be done when the value of B(1,j) is larger than any of the values from a certain colomn of A?
For example, B(1,1) = 4 and in the first colomn of A it is larger than A(1,1), A(3,1) and A(5,1), so these must be replaced by 4. In the second colomn, nothing needs to be replaced, etc.
Thanks already!
You can do this without any explicit looping using bsxfun:
A = [5 4 3 2 1
4 3 2 1 0
5 4 3 2 1
4 3 2 1 0
5 4 3 2 1];
B = [4 5 6 7 8];
A = bsxfun(#min,A,B);
Result:
A =
4 4 3 2 1
4 3 2 1 0
4 4 3 2 1
4 3 2 1 0
4 4 3 2 1
In later versions of MATLAB (2016b and later) you can even omit the bsxfun and get the same result.
A = min(A,B);
Matlab "find" may be of use to you.
https://www.mathworks.com/help/matlab/matlab_prog/find-array-elements-that-meet-a-condition.html
If you aren't concerned about speed or efficiency, you could also set up a two nested for loops with a condition (i.e. an if) statement comparing the values of A and B.
If you only interested in column wise comparison to B, you could use the increment of the outer loop in the inner loop.
for i,...
for j,...
if B(1,i) > A(j,i)
A(j,i)=B(i,j)

How to access n-D matrix with n index vectors? [duplicate]

This question already has answers here:
MATLAB: how to pass in the diagonal of a matrix as an argument in another matrix?
(2 answers)
Closed 6 years ago.
I have a matrix
A = repmat(1:7,7,1);
I have index vectors
idx1 = [1 3 5];
idx2 = [1 3 5];
I want to access A at the 2d coordinates denoted by idx1(i),idx2(i).
When I do
A(idx1,idx2) = 0;
I get for each element in idx 1, all the elements in idx2 as well.
I want only the corresponding elements to be assigned the zero value.
Again: I get
A =
0 2 0 4 0 6 7
1 2 3 4 5 6 7
0 2 0 4 0 6 7
1 2 3 4 5 6 7
0 2 0 4 0 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
but I want
A =
0 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 0 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 0 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
How to achieve this?
Thanks
The easiest way is probably to use sub2ind to generate the linear indices needed to index into A:
linear_ind = sub2ind(size(A),idx1,idx2);
A(linear_ind) = 0;

Loop through all combinations of 8 in 15 MATLAB

I have this matrix and want to make all combinations of column composed square matrixes (8x8) composed from this data.
4 2 4 3 2 3 3 2 8 4 9 7 6 6 6
2 0 4 1 0 3 0 8 5 0 9 3 7 7 1
2 1 2 1 1 3 1 4 5 2 4 2 6 6 3
0 0 2 2 1 2 3 9 1 1 4 4 4 4 6
4 0 1 0 4 2 3 1 8 1 3 0 5 5 7
3 1 4 0 0 1 0 2 6 2 9 1 2 2 0
1 2 1 4 0 3 4 1 3 4 3 9 7 7 9
2 0 0 4 0 0 3 1 5 0 1 9 1 1 7
Even after reeding Matlab Loop of all combinations
I'm not really sure how to do all the matrix combinations and include the counter from the for loop in the name of the combination obtained in the itteration.
I called your matrix A.
p=nchoosek(1:15,8);
gives all the combinations of 8 numbers taken from 1 to 15. These represent the columns of the matrix A that you want.
There are now 3 ways to proceed. Firstly, using a for loop:
M=zeros(8,8,size(p,1));
for i=1:size(p,1)
M(:,:,i)=A(:,p(i,:));
end
which puts each 8x8 matrix into a larger 3D array. You would get out individual matrices by doing M(:,:,54), for example.
You can also create a cell array:
N=arrayfun(#(k) A(:,p(k,:)),1:size(p,1),'UniformOutput',false);
and get individual matrices by doing N{54}.
Finally, you could not precompute each matrix, and just pull out the appropriate columns when you need them. This may be the most efficient method if you don't reuse the matrices:
O=A(:,p(54,:));

Choose specific values in matrix in MATLAB

I would like to select some numbers in a MATLAB matrix which have values greater than 4 and set them equal to zero.
For example:
A=[5 6 1 3 4 9 2 8 3];
Now, replace all values greater than 4 with zeros and store as a new matrix A1:
A1=[0 0 1 3 4 0 2 0 3];
You might want to try something like this:
A(A>4)=0
Here it is:
>> A=[5 6 1 3 4 9 2 8 3]
A =
5 6 1 3 4 9 2 8 3
>> A(A>4)=0
A =
0 0 1 3 4 0 2 0 3

How to duplicate all inner columns of a matrix and sum pairs of columns in Matlab

Suppose I have a matrix A
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
How do I duplicate the inner columns of A to get a new matrix B
1 2 2 3 3 4 4 5
1 2 2 3 3 4 4 5
1 2 2 3 3 4 4 5
1 2 2 3 3 4 4 5
1 2 2 3 3 4 4 5
Notice the first and last column of A were left alone. Then I need to sum pairs of rows together to get another matrix C:
3 5 7 9
3 5 7 9
3 5 7 9
3 5 7 9
3 5 7 9
The size of my matrices will not always be 5x5 and the elements will not always be so nice, but the matrix will always be square.
I do not need to generate or output matrix B. That was just simply how I initially thought of obtaining my final matrix C.
My goal is to be reasonably efficient, so I would like to accomplish this without a for loop.
How do I accomplish this for arbitrary matrix size nxn ?
Very simple . .
C = A(:,2:end) + A(:,1:end-1)