How to move a number of columns in a matrix to the right most in matlab - matlab

Suppose I have an mxn matrix A. Suppose I have a list (or a vector) P of i elements where each element of P indicates the number of a column in A. I need to move all the columns indicated by P to the right most of A; for instance the the columns indicated in the first and the i'th elements in P will become the (n-i)'th and the n'th column of A respectively.
Hope my statement is clear, let me know if there is any ambiguity.
thanks.

To get the columns 3,5,7 to the right, first c is constructed which indicates the new order of columns. Then the columns are indexed using c which reorders them.
>> M=magic(10);
>> c=[3,5,7];
>> c=[setdiff(1:size(M,2),c),c]
c =
Columns 1 through 9
1 2 4 6 8 9 10 3 5
Column 10
7
>> M=M(:,c);

Related

(MATLAB) How can I copy certain multiple elements from certain rows of a matrix based upon the values of other elements in those rows?

So, I have large matrix (let's say dimensions are 160x6 and the name of the matrix is datamatrix). Next, let's say I have another matrix called datamatrix2 which has dimensions 80x2. Here's what I want to do:
find every row of datamatrix where the value in column 2 is 2 and the value in column 5 is 1,
and then take the value for column 3 and the value from column 6 of each of those rows and place them in column 1 and column 2, respectively, of datamatrix2.
So, for example:
Let's say that row 3 of datamatrix is the first row in datamatrix with a 2 in column 2 and a 1 in column 5. Let's say there is a 3.096 in column 3 of that row and a 10 in column 6 of that row. So, 3.096 would be placed in position 1,1 of data matrix2 and 10 would be placed in position 1,2 of datamatrix2.
Next, let's say that row 25 of datamatrix is the next row in datamatrix with a 2 in column 2 and a 1 in column 5. Let's say there is a 16.432 in column 3 of that row and a 15 in column 6 of that row. So, 16.432 would be placed in position 2,1 of data matrix2 and 15 would be placed in position 2,2 of datamatrix2.
This process would continue until all of the rows of datamatrix with a 2 in column 2 and a 1 in column 5 have been found.
Please let me know if anyone has any suggestions.
Mucho thanks!
G
When I follow your explanation rightly, what you want is:
index = (datamatrix(:,2) == 2) & (datamatrix(:,5) == 1);
datamatrix2 = datamatrix(index,[4,6]);
This solution uses logical indexing.index stores 1's and 0's depending on wheter the condition is fullfilled or not.
To start of with logical indexing it is easiest to start with a vector. Lets take x = [2 5 3 4 6]; and you want the first, second and fifth entry. Then x([1 2 5]) = [2 5 6]; This can also be expressed logically with x(logical([1 1 0 0 1])) = [ 2 5 6]; Notice how we have a true at every position we want.
The same can be applied when we want to access a matrix.
Lets take A = [1 2 3; 4 5 6; 7 8 9] as a sample matrix. The obvious way to access part of this matrix is something like A([1,2],[1,3]) = [1 3; 4 6]. We can now replace the index with a logical notation:
A([1,2],[1,3]) = A(logical([1 1 0]),logical([1 0 1])) = [1 3; 4 6].
Logical and vector notation can also be combined. In my code index is a logical representation of the columns we want, [4 6] is the vector notation of the rows we want. Therefore the right columns and rows will be selected.
I don't really know what exactly you are trying, but lets take a shot
First: find any value (here 6) in any column (here 2) and then take another value of the found rows from any column (here 5) (here with an example magic-matrix)
datamatrix = [magic(5) magic(5)];
myvalue = 2;
values = datamatrix(find(datamatrix(:,2)==myvalue), 5);
If you have 2 conditions for a row, then use this here
values = datamatrix(find((datamatrix(:,column1)==value1).*(datamatrix(:,column2)==value2)),column3);
if you need to get more than one value, say from two different columns, then you just need to replace column3with something like this
column3 --> [col1 col2 col3]
Summarizing, try this code here
values = datamatrix(find((datamatrix(:,2)==2).*(datamatrix(:,5)==1)),[3 6]);
datamatrix2(:,[1 2]) = values
The second statement may fail because of dimension errors. If so, you should maybe provide the datamatrix entries.

Creating new matrix out of one column by order of two other columns

So I have a matrix A, take as an example
4 6 3.5
3 6 -1
A = 5 2 0.7
4 3 1.2
I now want to use Matlab to make a matrix B out of the last column of A in a very specific way. The rows of B should be ordered by the first column of A (in ascending order) and the columns of B should be ordered (ascending) by the second column of A. This can give empty elements in B, which should be assigned NaN. Applied to the example above this gives
NaN NaN -1
B = NaN 1.2 3.5
0.7 NaN NaN
Note that the number of rows and columns in B depend on the number of unique elements in the first and second column of A respectively.
I have tried a few different things trying to be clever with Matlab indexing but so far no success..
You can use this method
[~,J1,K1] = unique(A(:,1));
[~,J2,K2] = unique(A(:,2));
sz = [numel(J1) numel(J2)];
B = nan(sz);
B(sub2ind(sz, K1, K2)) = A(:,3);
first use unique to gather the unique items and their indices in the original column. The size of B is determined by number of unique elements in first and second column of A.
Now use linear indexing (obtained using sub2ind) to put the values in third column in the right place.

Create vector from elements other than diagonal ones

I would like to create a column vector from the elements of a matrix A of size (3,3) that are not on the diagonal. Thus, I would have 6 elements in that output vector. How can I do this?
Use eye and logical negation, although this is no better than Divakar's original answer, and possibly significantly slower for very large matrices.
>> A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> A(~eye(size(A)))
ans =
5
9
4
2
7
14
3
10
15
13
8
12
Use this to get such a column vector, assuming A is the input matrix -
column_vector = A(eye(size(A))==0)
If you don't care about the order of the elements in the output, you can also use a combination of setdiff and diag -
column_vector = setdiff(A,diag(A))
You can also use linear indexing to access the diagonal elements and null them. This will automatically reshape itself to a single vector:
A(1:size(A,1)+1:end) = [];
Bear in mind that this will mutate the original matrix A. If you don't want this to happen, make a copy of your matrix then perform the above operation on that copy. In other words:
Acopy = A;
Acopy(1:size(A,1)+1:end) = [];
Acopy will contain the final result. You need to create a vector starting from 1 and going to the end in increments of the rows of the matrix A added with 1 due to the fact that linear indices are column-major, so the linear indices used to access a matrix progress down each row first for a particular column. size(A,1) will allow us to offset by each column and we add 1 each time to ensure we get the diagonal coefficient for each column in the matrix.
Assuming that the matrix is square,
v = A(mod(0:numel(A)-1, size(A,1)+1) > 0).';

Delete row in a matrix Matlab

I have two matrices A "only has one column" and B which has more than one colomn but the same number of rows as A as shown below, what I want to do is to check the rows in A which has elements equal to -1 and remove delete this row from both matrices A and B, so in the example below I want to delete row index 3 and 6 from both matrices A and B, any advise?
A= 6
3
-1
6
6
-1
2
4
and B= -0.511774504646677 0.435674206557952 1.07400000000000
-0.509871997194459 0.437576714010170 1.07400000000000
-0.507969489742241 0.439479221462388 1.07400000000000
-0.506586007364545 0.429374013677012 1.07100000000000
-0.504201297562686 0.439754515167456 1.07100000000000
-0.501883219358233 0.428847974750132 1.07100000000000
-0.501415044713309 0.431930562861652 1.07100000000000
-0.499537085744345 0.433808521830616 1.07100000000000
Use logical indexing based on A:
B=B(A~=-1,:);
A=A(A~=-1);
Nearly the same as was proposed already, but without calculating the same index twice. Also this will not fail if you change the order of changing A and B.
idx = A~=-1;
A = A(idx);
B = B(idx);

MATLAB search row index of matrix with values of another matrix

In MATLAB I have a very large matrix (matrix A). Now I would like to find the row-index of the row which contain certain values in the second column. These values - which I'm looking for in Matrix A - are stored in anonther Matrix (Matrix B) with consists out of a row (800 numbers).
Besides I would like to redo this calculation for the same matrix A, but for ten different matrices, with different sizes (which contain the values I'm looking for in different columns of matrix A).
Because of the sizes of the matrix I think i need a loop to extract the row in matrix A which contain te value of Matrix B. How can I do this?
regards,
V
Thanks for the quick response! Indeed the problem is maybe a bit complex to answer without an example, and indeed duplicate entries cause some problems. Therefore hereby an example
For example I have a -simplified- matrix A:
1 2 3 4
9 9 9 9
4 3 2 1
And a -simplified- matrix (row) B: [9 3]
And a -simplified- matrix (row) C: [9 2]
Then I would like to get matrix D and matrix E.These matrices should contain in the first column the numbers from the original matrix D(or E) and in the second column the corresponding row-location of this value in matrix A.
So, matrix D =
9 2
3 3
matrix E =
9 2
2 3
As represented in this example matrix B and matrix C can contain data which is present in several column of matrix A (like the nine). However, martix B should "search" in column 2 of matrix A. Likewise, should matrix C "search" in column 3 of Matrix A, resulting in matrix D and E as given in the example.
As mentionned by Shai in his comment, your question is quite vague and a lot of special case could arise (duplicate entries, relative size of A and B, etc.). But in all generality I tried a small piece of code that seems to do what you want. There are certainly quicker ways of doing it, and certainly more information on your problem could help optimize this.
colA=2;
% Example
nmax=10;
nA=5;
A=randi(nmax,[nA nA]);
nB=3;
B=randi(nmax,[1 nB]);
% Find rows
rows=cell(size(B));
for i=1:numel(B)
rows(i)={find(A(:,colA)==B(i))};
end
The input / output was:
A =
3 7 8 5 4
9 7 3 7 5
8 2 9 9 8
9 5 9 7 9
3 3 4 6 8
B =
1 7 5
rows =
[0x1 double] [1;2] [4]
Assuming you have two vectors, largeDataIndex (the second column of your matrix) and interestingIndex (your b) and you want the following:
For each value of interestingIndex , find the position in largeDataIndex
Then an easy method would be this:
result = zeros(size(interestingIndex))
for i = 1:length(result)
result(i) = find(interestingIndex(i) == largeDataIndex)
end
Note that this assumes there is always just one entry that matches, otherwise you should define result as a cell array rather than a vector.