How to select only certain rows in a matrix in Matlab? - matlab

I have a matrix A in Matlab
A= [1 2 3 |1;
2 3 4 |2;
5 6 7 |2;
3 4 5 |1;
6 7 0 |3;
6 3 7 |3;
4 5 3 |1;
6 5 4 |4]
where the last column contains natural indices possibly repeated. For each index in the last column, I want to select the first row of A associated with that index and create the matrix
B=[1 2 3 |1;
2 3 4 |2;
6 7 0 |3;
6 5 4 |4]

Use unique to get the values and indices you require:
[U,I] = unique(A(:,4), 'first')
Then
A(I,:)

Related

Sorting two columns of a matrix while keeping one intact in OCTAVE/MATLAB

I have this matrix:
data=[1 5402783 1
2 43359352 2
3 26118700 3
4 33091887 4
5 890931 5
6 826897 6
7 1188749 7
8 1239861 8];
I need the first column to stay as it is, sort the 2nd column (in descending order) and 'keep along' the values of the third column. If I use sort(data) it sorts all 3 columns.
I tried:
[~,idx]=sort(data(:,2),'descend');
data=data(idx,:)
but it is obviously wrong.
The output should be:
[1 43359352 2
2 33091887 4
3 26118700 3
4 5402783 1
5 1239861 8
6 1188749 7
7 890931 5
8 826897 6]
All you need to do is reassemble the data matrix in the end taking the unsorted and sorted parts:
data = [1 5402783 1
2 43359352 2
3 26118700 3
4 33091887 4
5 890931 5
6 826897 6
7 1188749 7
8 1239861 8];
[~,idx] = sort(data(:,2),'descend');
data = [data(:,1),data(idx,2:3)];

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;

How to create a random 3D matrix?

Is there any way of creating a 3D matrix randomly? There are ways to create random 2D matrices using randint function. Is there any inbuilt function like that?
E.g. a 4x4 matrix can be generated easily by using the randint function. What if I want to create a matrix of dimension 4x4x3?
You can use randi(imax, size1, size2, size3) function where imax refers to maximum of random integer values (mean upper bound) and 1 is lower bound. You can expand size argument to sizeN what you want.
This is an example of its usage:
>> A = randi(5, 4, 4, 3)
A(:,:,1) =
4 4 5 4
4 1 2 2
2 1 3 3
4 3 2 4
A(:,:,2) =
5 1 5 1
5 2 2 2
3 5 5 4
1 2 2 3
A(:,:,3) =
2 5 2 3
5 2 3 4
3 4 1 5
3 4 1 1
If you read the help carefully, you will notice that the randi function accepts any number of dimensions. You may do randi(10,3,3,3)
randi(10,3,3,3)
ans(:,:,1) =
9 10 3
10 7 6
2 1 10
ans(:,:,2) =
10 10 2
2 5 5
10 9 10
ans(:,:,3) =
8 1 7
10 9 8
7 10 8

Remove zeros from matrix in 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))

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)