Combining Two Vectors Element-By-Element

Combining two vectors element-by-element

These commands create the vector:

X <- A
X[is.na(A)] <- B[is.na(A)]
X[is.na(B)] <- A[is.na(B)]
X[!is.na(A & B)] <- -1

#[1] 1 2 3 4 NA NA -1

Create a list by combining two vectors element-wise

You use Map -

Map(list, a, b)

#[[1]]
#[[1]][[1]]
#[1] 1

#[[1]][[2]]
#[1] "a"

#[[2]]
#[[2]][[1]]
#[1] 2

#[[2]][[2]]
#[1] "b"

#[[3]]
#[[3]][[1]]
#[1] 3

#[[3]][[2]]
#[1] "c"

Or map2 in purrr -

purrr::map2(a, b, list)

What is the best way to concatenate two vectors?

AB.reserve( A.size() + B.size() ); // preallocate memory
AB.insert( AB.end(), A.begin(), A.end() );
AB.insert( AB.end(), B.begin(), B.end() );

element-wise concatenation of string vectors

You can use paste or paste0:

> a <- c("a", "b", "c")
> b <- c("1", "2", "3")
> paste0(a, b)
[1] "a1" "b2" "c3"
>

Combining 2 vectors in alternating way by step of 4 in R

We can do this by splitting. Create a function to create a grouping variable with gl that increments at blocks of 'n' (here n is 4), then split both the vectors into a list, use Map to concatenate the corresponding list elements and unlist the list to create a vector

f1 <- function(x, n) split(x, as.integer(gl(length(x), n, length(x))))
unlist( Map(c, f1(a, 4), f1(b, 4)), use.names = FALSE)
#[1] 723 680 2392 2063 1 2 3 4
#[9] 721 746 2053 2129 5 6 7 8

Or if the lengths are the same, then we can rbind and concatenate after creating a matrix

c(rbind(matrix(a, nrow =4), matrix(b, nrow = 4)))
#[1] 723 680 2392 2063 1 2 3 4 721 746 2053 2129 5 6 7 8

Matlab: How to combine two vectors in one

I don't think that there is a built-in function that does exactly that, but the following will do what you want:

A=randi(10,1,320);
B=randi(10,1,192);
C=zeros(1,length(A)+length(B));
for i=1:5
C(i:8:end)=A(i:5:end);
end
for i=6:8
C(i:8:end)=B(i-5:3:end);
end

Then the array C is the combined array.

Edit: Another way to do that, without for loops:

A=randi(10,1,320);
B=randi(10,1,192);
A_new=reshape(A,5,[]);
B_new=reshape(B,3,[]);
C=[A_new;B_new];
C=reshape(C,[1,numel(C)]);

In this solution, by specifying the third parameter in reshape(A,5,[]) to be [], we allow it to adjust the number of columns according to the length of A, given that the number of rows in the reshaped array is 5. In addition, numel(C) is the total number of elements in the array C. So this solution can be easily generalized to higher number of arrays as well.

Pasting two vectors with combinations of all vectors' elements

You can use this, but there may be a simpler solution :

R> apply(expand.grid(vars, vis), 1, paste, collapse=".")
[1] "SR.1" "PL.1" "SR.2" "PL.2" "SR.3" "PL.3"

expand.grid gives back a data.frame which when used with apply, apply will convert it to a matrix. This is just unnecessary (and inefficient on large data). outer gives a matrix and also takes function argument. It'll be much efficient on huge data as well.

Using outer:

as.vector(outer(vars, vis, paste, sep="."))
# [1] "SR.1" "PL.1" "SR.2" "PL.2" "SR.3" "PL.3"

How do I combine two vectors of vectors element-wise in clojure?

(def a '[[c c c]
[y y y]
[m m m]])
(def b '[[r g b]
[r g b]
[r g b]])

(mapv (partial mapv vector) a b) ;; will work with arbitrary number
;; of equal sized arguments

;=> [[[c r] [c g] [c b]] [[y r] [y g] [y b]] [[m r] [m g] [m b]]]


Related Topics



Leave a reply



Submit