Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

#Day41 #100DaysChallenge- Matlab Loops|Adding numbers in pair from a vector

#Day41-Adding Numbers in Pair from a Vector
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
 A=1     2     3     4     5     6     7     8     9    10
 B=0     3     5     7     9    11    13    15    17    19     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(a)
b(1)=0;
j=2;
for i=1:1:length(a)-1
b(j)=a(i+1)+a(i);
j=j+1;
end
b(end+1)=0;



 Sample Input and Output
>> a=1:1:15

a =

     1     2     3     4     5     6     7     8     9    10    11    12    13    14    15

>> trail_add(a)

ans =

     0     3     5     7     9    11    13    15    17    19    21    23    25    27    29     0

Click Here for video Description






No comments

Popular Posts