  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # how to vectorise my code in r using for loop?  » r » how to vectorise my code in r using for loop?

By : Leon Bu
Date : October 16 2020, 08:10 AM
I hope this helps . Please read the guide to posting good questions on StackOverflow, it will allow people to answer your questions easily. You question is very confusing but this might give you some ideas: code :
`````` data %>%
group_by(Customer_ID, Store_Code) %>%
mutate(Predition298 = ifelse(dist.km298 > 20, 1, 0),
Predition299 = ifelse(dist.km299 > 20, 1, 0),
Predition300 = ifelse(dist.km300 > 20, 1, 0))
`````` ## Can someone help vectorise this matlab loop?

By : user3560793
Date : March 29 2020, 07:55 AM
I wish this helpful for you Matlab as a language does this type of operation poorly - you will always require an outside O(N) loop/operation involving at minimum O(K) copies which will not be worth it in performance to vectorize further because matlab is a heavy weight language. Instead, consider using the filter function where these things are typically implemented in C which makes that type of operation nearly free. ## Vectorise for-loop

By : Jacob Shekter
Date : March 29 2020, 07:55 AM
it should still fix some issue I need to replace this for-loop with better code: , I think this should work:
code :
``````    C1 = reshape(A(B.',:).', 6, []).';
``````
``````    %% Build minimal case
A = reshape(1:10, 5, 2);
B = randi(size(A,1), 7, 3);

%% Original code
for g=1:length(B)
C(g,:)=[A(B(g,1),:),A(B(g,2),:),A(B(g,3),:)];
end

%% Proposed code
C1 = reshape(A(B.',:).', 6, []).';

%% Test
disp(all(C1(:) == C(:)));
`````` ## How would you vectorise this for loop in R (s += i/2)?

Date : March 29 2020, 07:55 AM
code :
``````sum(seq_len(n)/2)

f(10)
# 27.5

sum(seq_len(10)/2)
# 27.5
``````
``````sum((1:n)/2)

n <- -11
f(n)
# -32.5

sum((1:n)/2)
# -32.5
``````
``````library(microbenchmark)
n <- 10000

f1 <- function(n) sum(seq_len(n)/2)
f2 <- function(n){ s <- 0;for (i in 1:n){s <- s + (i/2);};s}

f1(n)==f2(n)
#  TRUE
microbenchmark(f1(n), f2(n))

# Unit: microseconds
# expr      min       lq       mean   median       uq      max neval
# f1(n)   20.733   22.235   27.51751   22.836   24.639   82.028   100
# f2(n) 3971.008 4275.383 4517.52582 4484.510 4648.867 5867.272   100
`````` ## vectorise for loop with numpy.where

By : user6899764
Date : March 29 2020, 07:55 AM
it fixes the issue Use np.searchsorted to get the indices and then re-use those to get the invalid ones by comparing the indexed ones against a and set those as -1 -
code :
``````idx = np.searchsorted(b,a)
idx[b[idx] != a] =-1
`````` ## How can I vectorise this loop in MATLAB

By : Yesmeen Thomes
Date : March 29 2020, 07:55 AM
will help you Not sure if it's really faster (depends on Matlab's JIT) but you can try the following:
To find out which columns (equivalently, rows, since the matrix is symmetric) have more than one non zero element use: 