Reputation: 1267
Suppose I have an array of numbers.
S = [a_1,a_2,...,a_n]
How would I calculate the following function on each element in the array, and store the result in the adjacent cell next to the original element creating a 2x2 matrix?
T = [a_1, 1/(n+1); ...; a_i, i/(n+1); ...; a_n, n/n+1]
For the aforementioned matrix, 'i' represents the index, and 'n' represents the total number of elements. Is there a more efficient method than iterating through each element in a for loop? If I have to run this function on three such vectors, would a for loop be the best option?
Upvotes: 0
Views: 91
Reputation: 7805
This should do it, if I understood your question correctly:
n=length(S)
T=[S;(1:n)/(n+1)]'
Upvotes: 3