#Day12- #100DaysChallenge- Matlab Loops| matrix Pattern
#Day12- Matrix Pattern
Task:
Create a matrix which will produce output with following two condition:
1) The difference successive values in a row get incremented by 1 ,when the difference starts from 2 (First and second value difference).
2)Past row last value will be present row's first value .
Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those
Example
(a has 5 rows and 9 column)
a =
1 3 6 10 15 21 28 36 45
45 47 50 54 59 65 72 80 89
89 91 94 98 103 109 116 124 133
133 135 138 142 147 153 160 168 177
177 179 182 186 191 197 204 212 221
Matlab Code:
function a=rep_mat(x,y)
%
this function will produce output in a pattern where the difference
%
between number get incremented by 1 and difference starts from 2.in first
%
row last column value will be first column value of immediate next row
%examplea=rep_mat(5,9)
%a
=
% 1
3 6 10
15 21 28
36 45
% 45
47 50 54
59 65 72
80 89
%
89 91 94
98 103 109
116 124 133
%133 135
138 142 147
153 160 168
177
%177 179
182 186 191
197 204 212
221
k=1;
for i=1:1:x
k=k-1;
for j=1:1:y
a(i,j)=k+j;
k=k+j;
end
end
Sample Input and Output
a=rep_mat(3,9)
a =
1 3 6 10 15 21 28 36 45
45 47 50 54 59 65 72 80 89
89 91 94 98 103 109 116 124 133
>> a=rep_mat(10,12)
a =
1 3 6 10 15 21 28 36 45 55 66 78
78 80 83 87 92 98 105 113 122 132 143 155
155 157 160 164 169 175 182 190 199 209 220 232
232 234 237 241 246 252 259 267 276 286 297 309
309 311 314 318 323 329 336 344 353 363 374 386
386 388 391 395 400 406 413 421 430 440 451 463
463 465 468 472 477 483 490 498 507 517 528 540
540 542 545 549 554 560 567 575 584 594 605 617
617 619 622 626 631 637 644 652 661 671 682 694
694 696 699 703 708 714 721 729 738 748 759 771
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