Problem Statement:
Given a number n, create an n-by-n matrix in which the integers from 1 to n^2 wind back and forth along the rows as shown in the examples below.
Examples:
Input n = 3
Output a = [ 1 2 3 6 5 4 7 8 9 ]
Input n = 4
Output a = [ 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13 ]
CODE:
clc
x=input('Enter the number:');
m=1;
Y=[];
for i=1:x if(rem(i,2)==0) for j=x:-1:1 Y(i,j)=m; m=m+1; end else for j=1:x Y(i,j)=m; m=m+1; end end
end
disp(Y)
You can get the java code here:
https://www.matlabcoding.com/2019/06/...
Given a number n, create an n-by-n matrix in which the integers from 1 to n^2 wind back and forth along the rows as shown in the examples below.
Examples:
Input n = 3
Output a = [ 1 2 3 6 5 4 7 8 9 ]
Input n = 4
Output a = [ 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13 ]
CODE:
clc
x=input('Enter the number:');
m=1;
Y=[];
for i=1:x if(rem(i,2)==0) for j=x:-1:1 Y(i,j)=m; m=m+1; end else for j=1:x Y(i,j)=m; m=m+1; end end
end
disp(Y)
You can get the java code here:
https://www.matlabcoding.com/2019/06/...
Back and Forth Rows upload image | |
1 Likes | 1 Dislikes |
10 views views | 1.03K followers |
Science & Technology | Upload TimePublished on 6 Jun 2019 |
Không có nhận xét nào:
Đăng nhận xét