How to logically index entire columns in MATLAB - arrays

Given a logical column vector (size n x 1) v and an array a (size m x n) how do I generate a new array consisting of all the columns in a where the numerical index of said column (1...n) is 1 at the corresponding location in v.
So for example if v was
1
0
0
1
and a was
1 4 7 10
2 5 8 11
3 6 9 12
the new array would be
1 10
2 11
3 12
because the first and fourth elements of v are 1 (true), so the new array should contain the first and fourth columns of a.
I have tried a bunch of things involving normal logical indexing and transpose but I can't get it to work. All help is appreciated

You want to use the logical indexing to select the columns and select all rows. In the example below, I have explicitly cast v as a logical just in case it's not a logical matrix already.
new = a(:, logical(v))
1 10
2 11
3 12

Related

Determine max value of two matrices in single cell

I have two single column matrices named Matrix1 and Matrix2 in Excel, for instance:
Matrix1 Matrix 2
0 7
3 8
9 3
5 2
1 6
I want to determine the maximum value of the sums of each row and I think this is possible using an array formula but I can't get to a working solution. So basically what I want to do is the following, but in a single cell:
Matrix1 Matrix2 Matrix3
0 7 =index(Matrix1;1) + index(Matrix2;1)
3 8 =index(Matrix1;2) + index(Matrix2;2)
9 3 =index(Matrix1;3) + index(Matrix2;3)
5 2 =index(Matrix1;4) + index(Matrix2;4)
1 6 =index(Matrix1;5) + index(Matrix2;5)
Result = Max(Matrix3)
Is this possible in a singel cell?
Maybe for some inspiration, take a look at this post:
https://superuser.com/questions/373588/how-do-i-calculate-the-sum-of-2-columns-using-the-max-from-each-row
Try,
=MAX(INDEX((A2:A6)+(B2:B6), , ))

EXCEL VBA: Removing rows from an array and adding those rows to another array

I am working in EXCEL VBA.
I have a 2 dimensional array (for this example, let's say its a 5 x 5 one-based array). This is my raw data (Array "A"):
6 7 7 8 5
9 9 9 9 7
1 3 6 9 3
7 3 2 9 9
4 9 6 5 2
I also have a separate array whose row space mirrors that of the first (e.g., a 5 x 3 one-based array). The 1st column of this array is the row number of the raw data (A). This is my meta data (Array "B"):
1 0 0
2 1 0
3 0 0
4 0 0
5 1 0
For every occurrence of "1" in the 2nd column of the meta data array (B), I need to remove the corresponding row from my raw data array (A) AND add that row to a third array (Array "C")(which will not contain any data at the beginning of this process). Therefore, in this example, I need to remove rows 2 & 5 from Array A and place them in Array C.
I also need to copy the 1st column of the Array B (the original row numbers of Array A) to both arrays A & C so that after some further processing I can re-combine the results and return the data to its original order.
I'm not sure how best to go about this. Any suggestions?
Thanks!

Extracting array values based on values in different dimension

I've got a problem with subsetting values of an array.
raw.table <- array(data = c(1:12,13:24,rep(1:6, each=2)),
dim=c(3,4,3),
dimnames=list(LETTERS[1:3],1:4,c("target","ctrl","samples")))
The first two dimensions of my array represent some values that I want to do statistics on and the higher dimensions contain different attributes I want to use to access specific subsets. In this case I have only sample numbers, whereas there are always two values assigned to the same sample number (measurement replicates).
, , target
1 2 3 4
A 1 4 7 10
B 2 5 8 11
C 3 6 9 12
, , ctrl
1 2 3 4
A 13 16 19 22
B 14 17 20 23
C 15 18 21 24
, , samples
1 2 3 4
A 1 2 4 5
B 1 3 4 6
C 2 3 5 6
How do I access the values in dimension 1 (= target) that have the same sample number denoted in dimension 3 (= samples)? I tried out different approaches using unique(), duplicated() and match() but without coming to a result. I just cannot wrap my head about the indexing of arrays -.-
Cheers,
zuup
Form a logical index with a logical test (across dimensions):
> raw.table[,,1] == raw.table[,,3]
1 2 3 4
A TRUE FALSE FALSE FALSE
B FALSE FALSE FALSE FALSE
C FALSE FALSE FALSE FALSE
And use it to select items from the first dimension (and since they will be equal length there is no recycling):
> raw.table[, , 1 ][ raw.table[,,1] == raw.table[,,3] ]
[1] 1
Chaining calls to the Extract-operator is perfectly acceptable in R

sorting matrix in matlab based on another vector

I have a 2D matrix and want to sort rows and columns based on two other vectors i.e. one for ordering rows another for ordering columns in MATLAB
Example: A (Matrix to order)
0 1 2 3 4
1 1 8 9 7
2 3 4 6 2
3 1 2 0 8
Row Vector (Order for sorting rows of matrix A)
1
4
2
3
And column vector
1 5 4 2 3
Modified A
0 4 3 1 2
3 8 0 1 2
1 7 9 1 8
2 2 6 3 4
How about:
ModifiedA=A(RowVector,ColumnVector);
Note: Matab's indexing starts at 1 not at 0, adapt your indexing vectors accordingly.
In MATLAB, you can use the second output of sort to get the 1-based indexes that MATLAB is looking for (in this case you could have just added 1, but using sort works even if the row and column vectors are not consecutive).
[~,rowIdx] = sort(rowVector);
[~,colIdx] = sort(colVector);
And then you can apply the indexing operation to the matrix:
modifiedA = A(rowIdx, colIdx);

Matlab : Matrix indexing Logic

i am doing very simple Matrix indexing examples . where code is as give below
>> A=[ 1 2 3 4 ; 5 6 7 8 ; 9 10 11 12 ]
A =
1 2 3 4
5 6 7 8
9 10 11 12
>> A(end, end-2)
ans =
10
>> A(2:end, end:-2:1)
ans =
8 6
12 10
here i am bit confused . when i use A(end, end-2) it takes difference of two till first column and when there is just one column left there is no further processing , but when i use A(2:end, end:-2:1) it takes 6 10 but how does it print 8 12 while there is just one column left and we have to take difference of two from right to left , Pleas someone explain this simple point
The selection A(end, end-2) reads: take elements in last row of A that appear in column 4(end)-2=2.
The selection A(2:end, end:-2:1) similarly reads: take elements in rows 2 to 4(end) and starting from last column going backwards in jumps of two, i.e. 4 then 2.
To check the indexing, simply substitute the end with size(A,1) or size(A,2) if respectively found in the row and col position.
First the general stuff: end is just a placeholder for an index, namely the last position in a given array dimension. For instance, for an arbitrary array A(end,1) will pick the last element in column 1, and A(1,end) will pick the last element in the first row.
In your example, A(end, end-2) picks an element in the last row two columns before the last one.
To interpret a statement such as
A(2:end, end:-2:1)
it might help to substitute end with the actual index of the last row/column elements, so this is equivalent to
A(2:3, 4:-2:1)
Furthermore 4:-2:1 is equivalent to the list 4,2 since we are instructing to make the list starting at 4, decreasing in steps of 2, up to (minimum) 1. So this is equivalent to
A([2 3],[4 2])
Finally, the following combination of indices is implied by A([2 3],[4 2]):
A(2,4) A(2,2)
A(3,4) A(3,2)

Resources