Selecting elements in matrix using matlab - matlab

My problem is this: I've a matrix, as example
1 2 3
4 2 6
6 1 8
4 5 4
7 1 5
8 2 0
I wish to extract selected values from the matrix, as example, a vector like this
B = [3 6 0]
selecting third column values when the value in the second column is 2.
I tried in different ways, but no one of these works.

Use this -
B = A(A(:,2)==2,3)' %// Assuming A is your input matrix

If M is your Matrix, you can select the second column using
M(:,2)
Compare it to two to get the lines which contain a 2
M(:,2)==2
And use this logical vector to select your elements from the third column.
M(M(:,2)==2,3)

A little more generally: if you want to select based on a set of values, use ismember to generate the logical index:
>> A(ismember(A(:,2), [2 5]) , 3) %// [2 5]: values you want to find in 2nd col
ans =
3
4
6
0

Related

mean of two matrixes with Nans, element per element

I want to have the nanmean of two matrixes element per element. I don't seem to be able to do it using the function nanmean, since that doesn't sum element per element.
Simplified example:
A= [1 1 1
1 1 1
1 nan 1];
B=[3 3 3
3 3 3
3 3 3];
Result I want :
C= [2 2 2
2 2 2
2 3 2];
So the nan is ignored.
One way to sum element per element I found on https://nl.mathworks.com/matlabcentral/answers/366304-how-to-sum-up-multiple-matrices-element-by-element
A(:,:,1)=randi([1 3],100,100);
A(:,:,2)=randi([1 3],100,100);
A(:,:,3)=randi([1 3],100,100);
A(:,:,4)=randi([1 3],100,100);
B=zeros(size(A,1),size(A,2));
for i=1:size(A,3)
B=B+A(1:size(A,1),1:size(A,2),i);
end
disp(B)
BUT this isn't the nansum. How can I do this not taking into account the nans?
You can concatenate the matrices along the 3rd dimension, then apply nanmean along that dimension:
C = cat(3,A,B);
C = nanmean(C,3);
(Of course you can write these two statements as a single one, I wrote it like this for clarity.)

Mixing columns in matlab

I have a 2x100 matrix. It contains 100 elements from 2 different classes. So each element consists of the value itself and the label with the class it belongs to(1 or 2). I want to mix this data into another 2x100 matrix, where the values stay still connected to their labels.
An example with a 2x5 matrix would be:
A=[1 2 3 4 5;
1 2 2 2 1]
After mixing:
A=[2 3 5 1 4;
2 2 1 1 2]
How can I do this? Thanks!
You can index the entire columns (and randomly change the order using randperm)
Amix = A( :, randperm(size(A,2)) );
See an example at ideone.

Duplicate the first row and the first column of a matrix

I have a 3*3 matrix A
A = [1 2 3
4 5 6
7 8 9];
I want to duplicate only the first row and column of this matrix. It should look like
1 1 2 3
1 1 2 3
4 4 5 6
7 7 8 9
can someone tell how can i do this in matlab
I think this is a good way just using indexing
A([1, 1:end], [1, 1:end])
You can do that by concatenating different parts of the original matrix:
B=[A(1) A(1,:);A(:,1) A];
In this expression A(1) is the top left element of A, A(1,:) is the first row and A(:,1) is the first column.
See the documentation on the colon operator.
In the code below, A is your starting point and I believe E is what you want to achieve.
You can of course combine all the intermediate expressions to achieve the final result in one step.
A= [1 2 3; 4 5 6; 7 8 9]
B= A(1:3,1:1)
C= [B A]
D= C(1:1,1:4)
E= [D;C]
A bit late in the game, but worthwhile answering. You can use padarray for that :
B = padarray(A,[1 1],'replicate','pre')
It's a one liner and more generic if you want to add more than just a single first and column ...

Sorting entire matrix according to one column in matlab

I have the matrix as follows
a =
1 3
2 5
3 2
4 8
5 9
I want to sort the second column in the a matrix. I want the corresponding rows of column one to be printed as follows :
a =
3 2
1 3
2 5
4 8
5 9
I tried sort(a), but it is sorting only the second column of matrix a.
Try this:
sortrows(a,2)
This should sort according to the second column.
or use:
[val idx]=sort(a(:,2));
ans = [a(idx,1) val]

Calculate mean of columns only

I have a function that calculates the mean of two columns of a matrix. For example, if the following matrix is the input:
inputMatrix =
1 2 5 3 9
4 6 2 3 2
4 4 3 9 1
... And my command is:
outputVector = mean(inputArray(:,1:2))
...Then my output is:
outputVector =
3 4
The problem arises when my input matrix only contains one row (i.e. when it is a vector, not a matrix).
For example, the input:
inputMatrix =
4 3 7 2 1
Gives the output:
outputVector =
3.5000
I would like the same behaviour to be maintained regardless of how many rows are in the input. To clarify, the correct output for the second example above should be:
outputVector =
4 3
Use the second argument of MEAN to indicate along which dimension you want to average
inputMatrix =[ 4 3 7 2 1]
mean(inputMatrix(:,1:2),1) %# average along dim 1, i.e. average all rows
ans =
4 3
mean(blah, 1)
See the documentation: http://www.mathworks.co.uk/help/techdoc/ref/mean.html.