I have searched for an Octave function that facilitates conditional merging of matrices but haven't one so far. My goal is to do this using vectors without looping. Here is an example of what I am trying to do.
A= [1 1
2 2
3 1
5 2];
B= [1 9
2 10];
I would like to get C as
C= [1 1 9
2 2 10
3 1 9
5 2 10];
Is there a function that takes A, B and the list of column(s) to join on and then produce C?
You can use the second output of ismember to find the occurrences of the second column of A in the first column of B and then use that to grab specific entries from the second column of B to construct C.
[~, inds] = ismember(A(:,2), B(:,1));
C = [A, B(inds,2)];
%// 1 1 9
%// 2 2 10
%// 3 1 9
%// 5 2 10
Related
I wish to make a formula to sum up the value with 2 criteria, example show as below:-
A B C D E
1 1-Apr 2-Apr 3-Apr 4-Apr
2 aa 1 4 7 10
3 bb 2 5 8 11
4 cc 3 6 9 12
5
6 Criteria 1 bb
7 Range start 2-Apr-16
8 Range End 4-Apr-16
9 Total sum #VALUE!
tried formula
1 SUMIF(A2:A4,C6,INDEX(B2:E4,0,MATCH(C7,B1:E1,0)))
* Only return 1 cell value
2 SUMIF(A2:A4,C6,INDEX(B2:E4,0,MATCH(">="&C7,B1:E1,0)))
* Showed N/A error
3 SUMIFS(B2:E4,A2:A4,C6,B1:E1,">="&C7,B1:E1,"<="&C8)
* Showed #Value error
Hereby I attached a link of picture for better understanding :
Can anyone help me on the formula?
I figured out the solution with step evaluation:
=SUMIF(B1:F1,">="&C7,INDEX(B2:F4,MATCH(C6,A2:A4,0),0)) -
SUMIF(B1:F1,">"&C8,INDEX(B2:F4,MATCH(C6,A2:A4,0),0))
I have two dataframes df1 and df2 I want to join. Their indexes are not the same and they don't have any common columns. What I want is to join them based on the order of the rows, i.e. join the first row of df1 with the first row of df2, the second row of df1 with the second row of df2, etc.
Example:
df1:
'A' 'B'
0 1 2
1 3 4
2 5 6
df2:
'C' 'D'
0 7 8
3 9 10
5 11 12
Should give
'A' 'B' 'C' 'D'
0 1 2 7 8
3 3 4 9 10
5 5 6 11 12
I don't care about the indexes in the final dataframe. I tried reindexing df1 with the indexes of df2 but could not make it work.
You could assign to df1 index of df2 and then use join:
df1.index = df2.index
res = df1.join(df2)
In [86]: res
Out[86]:
'A' 'B' 'C' 'D'
0 1 2 7 8
3 3 4 9 10
5 5 6 11 12
Or you could do it in one line with set_index:
In [91]: df1.set_index(df2.index).join(df2)
Out[91]:
'A' 'B' 'C' 'D'
0 1 2 7 8
3 3 4 9 10
5 5 6 11 12
Try concat:
pd.concat([df1.reset_index(), df2.reset_index()], axis=1)
The reset_index() calls make the indices the same, then, concat with axis=1 simply joins horizontally.
I guess you can try to join them (doing this it performs the join on the index, which is the same for the two DataFrame due to reset_index):
In [18]: df1.join(df2.reset_index(drop=True))
Out[18]:
'A' 'B' 'C' 'D'
0 1 2 7 8
1 3 4 9 10
2 5 6 11 12
I know that tensors have an apply method, but this only applies a function to each element. Is there an elegant way to do row-wise operations? For example, can I multiply each row by a different value?
Say
A =
1 2 3
4 5 6
7 8 9
and
B =
1
2
3
and I want to multiply each element in the ith row of A by the ith element of B to get
1 2 3
8 10 12
21 24 27
how would I do that?
See this link: Torch - Apply function over dimension
(Thanks to Alexander Lutsenko for providing it. I just moved it to the answer.)
One possibility is to expand B as follow:
1 1 1
2 2 2
3 3 3
[torch.DoubleTensor of size 3x3]
Then you can use element-wise multiplication directly:
local A = torch.Tensor{{1,2,3},{4,5,6},{7,8,9}}
local B = torch.Tensor{1,2,3}
local C = A:cmul(B:view(3,1):expand(3,3))
Given a complete dense graph (over 250.000 nodes) , what is the quickest way to determine the number of k-length paths from node A to B ?
I understand this is an old post, but I had the exact same question and could not find the answer.
I like to think of this problem as a "permutation without repetition", as the order of the nodes visited matters (permutation) and we aren't backtracking (no repetitions). The number of permutations without repetition is: n!/(n-r)!
For a complete graph with N nodes, there are N - 2 remaining nodes to choose from when creating a path between a given A and B. To create a path of length K, K-1 nodes must be chosen from the remaining nodes after A and B are excluded. Therefore, in this context, n = N - 2, and r = k - 1.
Plugging into the above formula yields:
(N-2)!/(N-K-1)!
Example: for N = 5, with nodes 0,1,2,3,4 the following paths are possible from 0 to 1:
0 1
0 2 1
0 2 3 1
0 2 3 4 1
0 2 4 1
0 2 4 3 1
0 3 1
0 3 2 1
0 3 2 4 1
0 3 4 1
0 3 4 2 1
0 4 1
0 4 2 1
0 4 2 3 1
0 4 3 1
0 4 3 2 1
This yields 1 path of length 1, 3 paths of length 2, 6 paths of length 3, and 6 paths of length 4.
This appears to work for any N>=2 and K<=N-1.
You can use basically dynamic programming: For each node Y and path length k, you can compute the number of paths from A to Y of length k if you know the number of paths from A to X of path length k-1 for all nodes X. Total complexity is O(KV), where K is the total path length you are trying to compute for and V is the number of vertices.
I have written this formula below. I do not know the correct part of this formula that will add the numbers I have in Column AB2:AB552. As it is, this formula is counting the number of cells in that range that has numbers in it, but I need it to total those numbers as my final result. Any help would be great.
=COUNTIFS(Cases!B2:B552,"1",Cases!G2:G552,"c*",Cases!X2:X552,"No",**Cases!AB2:AB552,">0"**)
Assuming you don't actually need the intermediate counts, the sumifs function should give you the final result:
=SUMIFS(Cases!AB2:AB552,Cases!B2:B552,1,Cases!G2:G552,"c",Cases!X2:X552,"No",Cases!AB2:AB552,">0")
Testing this with some limited data:
Row B G X AB
2 2 a No 10
3 1 c No 24
4 2 c No 4
5 1 c No 0
6 1 a Yes 9
7 2 c No 12
8 2 c No 6
9 2 b No 0
10 1 b No 0
11 1 a No 10
12 2 c No 6
13 1 c No 20
14 1 c No 4
15 1 b Yes 22
16 1 b Yes 22
the formula above returned 48, the sum of AB3, AB13, and AB14, which were the only rows matching all 4 criteria