#Day42#100DaysChallenge- Matlab Loops|Adding numbers in pair from a vector-Iteration
#Day42-Adding Numbers in Pair from a Vector-Iteration with resultant Vector
Task:
Task:
Write a code for adding numbers in pair in a vector(A), user input is vector(A) whose entry is added in pair and also add zero at the end and start position of vector B.Do the process for 4 times with resultant vector and return the answer as a matrix.
a=[1,2,3,4]
0 3 5 7 0 0 0 0
0 3 8 12 7 0 0 0
0 3 11 20 19 7 0 0
0 3 14 31 39 26 7 0
Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those
Matlab code
        
a=[1,2,3,4]
0 3 5 7 0 0 0 0
0 3 8 12 7 0 0 0
0 3 11 20 19 7 0 0
0 3 14 31 39 26 7 0
Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those
Matlab code
function b=trail_add_loop(a)
for k=1:1:4
    b(k,1)=0;
    j=2;
    for i=1:1:length(a)-1
        b(k,j)=a(i+1)+a(i);
        j=j+1;
    end
        b(k,end+1)=0;
        a=b(k,:);
end
 Sample Input and Output
>> a=[1:1:9]
a =
     1     2     3     4     5     6     7     8     9
>> trail_add_loop(a)
ans =
     0     3     5     7     9    11    13    15    17     0     0     0     0
     0     3     8    12    16    20    24    28    32    17     0     0     0
     0     3    11    20    28    36    44    52    60    49    17     0     0
     0     3    14    31    48    64    80    96   112   109    66    17     0
Click here for Video Description
 
 
 
 
 
 
 
%206th%20Edition,%20Kindle%20Edition.jpg) 
 
 
 Posts
Posts
 
 
 
 
 
 
 
 
No comments