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

Search This Blog

#Day13- #100DaysChallenge- Matlab Loops| Numberline-Matrix Pattern

#Day13- Numberline-Matrix Pattern


Task: Create a Number line matrix, First row of the matrix should be positive values(i.e) Right side of the zero Number of positive values to be taken is according to the number of column when it moves to new row has to incorporate one value from the left side of the first row and one value in the right.

The beauty of this Matrix pattern is you will notice a positive upper triangle and negative lower triangle.
and if it square matrix you will find all the diagonal elements to be 1.

Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those
Matlab code:

function a=numberline_mat(x,y)
%this the function will generate a matrix which starts from 1, the number of columns
%will depend on y value on the second row the first entry should move the left side
%of the number line by one step.
%EXAMPLE
% a=numberline_mat(5,5)

%a =

   %   1     2     3     4     5
   %  0     1     2     3     4
   % -1     0     1     2     3
   % -2    -1     0     1     2
   % -3    -2    -1     0     1

k=1;
for i=1:1:x
    k=k-1;
    for j=1:1:y
        a(i,j)=k+j;
    end
end


Sample Output and Input
 a=numberline_mat(5,6)

a =

     1     2     3     4     5     6
     0     1     2     3     4     5
    -1     0     1     2     3     4
    -2    -1     0     1     2     3
    -3    -2    -1     0     1     2

>> a=numberline_mat(8,8)

a =

     1     2     3     4     5     6     7     8
     0     1     2     3     4     5     6     7
    -1     0     1     2     3     4     5     6
    -2    -1     0     1     2     3     4     5
    -3    -2    -1     0     1     2     3     4
    -4    -3    -2    -1     0     1     2     3
    -5    -4    -3    -2    -1     0     1     2
    -6    -5    -4    -3    -2    -1     0     1
Click Here for Video Description


Join us on Telegram:https://t.me/matlabirawen
 Join us on Facebook Group: https://www.facebook.com/groups/matlabcodes

No comments

Popular Posts

Followers