Problem 57540. Repeatitive Summation
Given numeric vectors x, a and b, perform the following operation -
a represents a vector of numbers and b represents a vector of indices; corresponding to the values in b, add the values of a to x.
Examples -
Input
x = [1 2 3 4]
a = [1 2 3]
b = [1 2 1]
Output
y = [5 4 3 4]
Explaination - Add a(1) to x(b(1)), a(2) to x(b(2)) and so on.
Input
x = [3; 5; 7; 9]
a = [-2; 0; 2]
b = [4; 4; 1]
Output
y = [5; 5; 7; 7]
Note - Values in x and a will belong to Real numbers, values in b will be Integers in the range [1, numel(x)]. Test suite will be updated regularly.
Restrictions - loops and recursion are not allowed.
Solution Stats
Problem Comments
-
2 Comments
David Hill
on 29 Jan 2023
I am very confused by your explanation. b will be integers in the range [1, numel(x)], doesn't the second example violate this (where b has -2 and 0 as members)? I still don't see how you are getting your output with the explanation provided.
Dyuman Joshi
on 30 Jan 2023
There was some mishap in that example, David. b was supposed to be a (and vice-versa), as you pointed out the violation.
It has been cleared out. I hope the question is clear now.
Solution Comments
Show commentsProblem Recent Solvers7
Suggested Problems
-
471 Solvers
-
We love vectorized solutions. Problem 1 : remove the row average.
823 Solvers
-
598 Solvers
-
5182 Solvers
-
440 Solvers
More from this Author31
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!