# how to replace elements in top third, middle third, and bottom third of matix

1.495 views (last 30 days)
Marlon Izaguirre on 1 May 2019
Commented: mahmoud khaled on 11 Aug 2020 at 12:05
Write a function called trio that takes two positive integer inputs n and m. The function returns a 3n-by-m matrix called T. The top third of T (an n by m submatrix) is all 1s, the middle third is all 2-s while the bottom third is all 3-s. See example below:
M = trio(2,4)
M =
1 1 1 1
1 1 1 1
2 2 2 2
2 2 2 2
3 3 3 3
3 3 3 3
This is the code that I wrote, but it only works for T = trio (4,3). I want my code to work for any input of n,m.
function T = trio (n, m)
T = randi (10, (3 * n) , m);
T ( 1:n , :) = 1;
T ( (n+1):(end-(n-1)) , :) = 2;
T ( (n+3):end, :) = 3;
end
How is it possible to call out only top third, middle third, and bottom third of any matrix?

MD. AL NAHIAN SAYEM on 17 Jun 2020
For random input use k=trio(round(rand),round(rand))
Sai Swaroop Maram on 28 Jul 2020 at 7:35
function M=trio(n,m)
p=3*n;
q=(1/3)*p;
M=[ones(q,m);2*ones(q,m);3*ones(q,m)];
mahmoud khaled on 11 Aug 2020 at 12:05
function T=trio(n,m)
T=[ones(n,m) ; (2*ones(n,m)) ; (3*ones(n,m))];
end

James Tursa on 1 May 2019
Edited: James Tursa on 1 May 2019
The first n rows are 1:n which you have correct.
The second n rows indexing is n more that the first set, so simply add n: n + (1:n)
The third n rows indexing is n more than the second set, which I will let you figure out (it's pretty simple)

Marlon Izaguirre on 1 May 2019
Thank you so much James!!!! I was able to figure out the third n rows.
Ajay Raineni on 4 Apr 2020
Pls can you help me

AYUSH GURTU on 28 May 2019
function T = trio (n, m)
T = randi (10, (3 * n) , m);
T (1:n,:) = 1;
T ((n+(1:n)),:) = 2;
T (n+(n+(1:n)):end,:) = 3;
end

#### 1 Comment

sona rai on 9 Aug 2020 at 2:41
function T=trio(n,m)
T=randi(10,(3*n),m);
T(1:n,:)=1;
T((n+(1:n)),:)=2;
T((n+(n+(1:n))),:)=3;
end
t

PRAKASH ANAND on 8 Nov 2019
% That's my trio code.
%From India.
function T=trio(n,m)
x=ones(n,m);
y=2*x;
z=3*x;
T=[x;y;z];
end

Show 1 older comment
Danish Dutt Attrey on 28 May 2020
To get a matrix with all the numbers which are one
Rafael Eduardo Pachas Vega on 4 Aug 2020 at 15:50
awesome
vanam sindhuja on 10 Aug 2020 at 15:46
It's so easy and presence of mind,smart thinking.Thank you

evan muas on 2 Dec 2019
function T=trio(n,m)
T=[ones(n,m);2*ones(n,m);3*ones(n,m)]
end

Stephen Cobeldick on 17 Apr 2020
+1 straightfoward solution. I might as well leave this general solution here too:
>> trio = @(n,m) kron([1;2;3],ones(n,m));
>> trio(2,4)
ans =
1 1 1 1
1 1 1 1
2 2 2 2
2 2 2 2
3 3 3 3
3 3 3 3
Upoma Saha on 19 Jul 2020 at 13:44
this is abosulely amazing.
Akinola Tomiwa on 25 Jul 2020 at 16:32
Nice

mayank ghugretkar on 5 Jun 2019
function T=trio(n,m)
T(3*n,m)=3; % or you can use random no. generation...but since we are assigning alues anyway , this vl work fine !
T(1:n,:)=1;
T((n+1):2*n,:)=2;
T((2*n+1):3*n,:)=3;
end
hope this'll help, welcome !

#### 1 Comment

SWAMI NAIDU BODDU on 1 Jun 2020
Perfect code for random inputs also...... Thanks you sirr!!.....

Doga Savas on 22 Aug 2019
function d = trio(n,m)
a = randi(1,n,m);
b = 2 + rand(n,m)*0;
c = 3 + rand(n,m)*0;
d = [a;b;c];
end

function T = trio (n, m)
T = randi (10, (3 * n) , m);
T ( 1:n , :) = 1;
T ( (n+1):(2*n) , :) = 2;
T ( ((2*n)+1):end, :) = 3;
end

mohammad elyoussef on 2 Apr 2020
function F = trio (n, m)
T = ones ( n , m);
F = [ T; 2.*T; 3.*T];
end
Jayanta Sarkar on 4 Jul 2020

function T = trio (n,m) T = randi(10,(3 * n),m); T (1:n, :) =1; T ( (n+1):(2*n) ,;) =2; T ( ((2*n)+1):end :) =3; end

samyak doshi on 5 Apr 2020
function T= trio(n,m);
n=3*n;
T1= ones(n/3,m); %ones for getting 1 in as all the elements in that respective row and column
T2= 2*ones(n/3,m); % getting 2's
T3= 3*ones(n/3,m); %getting 3's
T= [T1;T2;T3]; %full matrix of 1,2 and 3's

Bece on 17 Apr 2020
why are you multiplying and then dividing n by 3 ?
Saradha Manikandan on 19 May 2020
since in the problem statement it has been mentioned "the function returns a 3n-by-m matrix ".In order to make n as 3n it has been multiplied and the rest you may guess

Kulasekaran N on 12 Jun 2020
function T = trio(n,m)
T = [ones(n,m);2*ones(n,m);3*ones(n,m)];
end

Bruno Luong on 19 Jul 2020 at 13:59
Edited: Bruno Luong on 19 Jul 2020 at 14:14
Add my own contribution to the long list of possible solutions
trio=@(n,m)repmat(ceil((1:3*n)'/n),1,m)

Yihan Liu on 23 Sep 2019
Edited: Yihan Liu on 23 Sep 2019
function T= trio(n,m)
T = randi (10, n, m);
T(:) = 1;
T1 = T;
T(:) = 2;
T2 = T;
T(:) = 3;
T3 = T;
T = [T1;T2;T3];
end

Sowmya Allani on 24 Sep 2019
function T=trio(n,m)
T=randi(3,3*n,m)
[n,m]=size(T)
x=n/3;
T(1:x,:)=1;
T(x+1:x*2,:)=2;
T((x*2)+1:x*3,:)=3;
end

Akash Devalekar on 16 Dec 2019
function T=trio(n,m)
w=ones(n,m);
y=ones(n,m)+1;
x=ones(n,m)+2;
T=[w;y;x];
end

SADIQ GBAGBA on 4 Mar 2020
function[T]= trio(n,m)
T=ones(3*n,m);
T(n+1:2*n,:)=2;
T(n+n+1:3*n,:)=3;
end

Irfan Hussain on 29 Mar 2020
function T = trio (n, m)
T = ones((3 * n) , m);
T ( 1:n , :) = 1;
T ( (n+1):(end-(n-1)) , :) = 2;
T ( (end-(n-1)):end, :) = 3;
end

Priya on 14 Apr 2020
function T = trio(n,m)
T = randi (10, (3 * n) , m);
T ( 1:n , :) = 1;
T ( (n+1):(end-(n-1)) , :) = 2;
T ( (2*n+1):end, :) = 3;
end

Vivek Mokal on 14 Apr 2020
function T = trio (n,m)
v=3*n;
T=rand(v,m);
T(1:n,1:end)=1;
y=floor(n+n);
T((n+1):y,1:end)=2;
T((y+1):end,1:end)=3;
end

Siddharth Joshi on 22 Apr 2020
Edited: Siddharth Joshi on 22 Apr 2020
function T = trio(n,m)
M=randi(10,3*n,m);
M([1:n],:)=1;
M([n+1:2*n],:)=2;
M([2*n+1:3*n],:)=3
end
trio(3,6)
T =
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
2 2 2 2 2 2
2 2 2 2 2 2
2 2 2 2 2 2
3 3 3 3 3 3
3 3 3 3 3 3
3 3 3 3 3 3

Vishal Natraj on 23 Apr 2020
function T=trio(n,m)
zeros(3*n,m);
T((1:n),(1:m)) = 1;
T((n+1):(2*n),(1:m)) = 2;
T((2*n)+1:(3*n), (1:m)) = 3;
end

Ali Badr on 29 Apr 2020
function T = trio (n, m)
T =rand(n,m);
T (1:n,:) = 1;
T ((n+1:2*n),:) = 2;
T ((2*n+1):3*n,:) = 3;
end

Arooba Ijaz on 1 May 2020
function [T]= trio (n,m)
a=rand([3*n,m])
a(1:end/3,:)=1
a(end/3+1:end*2/3,:)=2
a(end*2/3+1:end,:)=3
T=a

Deep Raj Bhujel on 6 May 2020
function T=trio(n,m)
T=randi(10,3*n,m);
T(1:n,:)=1;
T(n+1:2*n,:)=2;
T(2*n+1:end,:)=3;
end

kannan vidyadhar on 6 May 2020
function t=trio(n,m)
n=input('enter number:')
m=input('enter number:')
t=ones(3*n,m);
g=ones(n,m);
h=ones(n,m)+1;
j=ones(n,m)+2;
t=[g;h;j]
end

ERTIZA HOSSAIN SHOPNIL on 6 May 2020
function T=trio(n,m)
x= ones(n,m);
y=1+ones(n,m);
z=2+ones(n,m);
T=[x;y;z];
end

Taif Ahmed BIpul on 9 May 2020
function T=trio(n,m)
T=[ones(n,m);ones(n,m)*2;ones(n,m)*3];
end

Qaiser khan Bozdar on 10 May 2020
Hi Bro
My following code is not taking random integers kindly review my code and give me suggestions....
You are code is running good, you are merging all matrices in single T and getting target goal.
function T=trio(n,m)
T=ones(3*n,m);
T([3,4],:)=2;
T([5,6],:)=3;
end
the input (2,4) is running good but in other inputs it only goes to above 1 to 6 or 1 to 3 rows with all coulmns.
I think i`ve doubt on rows that, how i am coding them?
Ashitha Nair on 17 Jun 2020
Your code is only correct when n=2, but for all other n's (if n=3, first three rows should be equal to one, second three row elements should be equal to 2 and the last three should be equalt to 3, and so forth so the code you've written i.e T([3,4], : )= 2; becomes incorrect in this case, your code will not result in the required matrix. Hence, to code for the required matrix irrespective of the n, you have to form a general format or follow the code above. Hope it helps.

Rahmi Elagib on 12 May 2020
T = randi (10, n, m);
T(:) = 1;
T1 = T;
T(:) = 2;
T2 = T;
T(:) = 3;
T3 = T;
T = [T1;T2;T3];

MD.Ismail Emon on 15 May 2020
function T = trio(n,m)
T = randi(10,3*n,m);
T(1:n,:) = 1;
T(n+1:(end-n),:) = 2;
T(end-n+1:end,:) = 3;
end

muyiwabowen on 19 May 2020
function T = trio (n, m)
T = ones((3 * n) , m);
T (1:n,:) = 1;
T (n+1:2*n,:) = 2;
T (2*n+1:3*n,:) = 3;
end

AYUSH MISHRA on 27 May 2020
function T=trio(n,m)
T=randi(10*n,3*n,m);
T(1:n,1:m)=1;
T(n+1:2*n,1:m)=2;
T(2*n+1:3*n,1:m)=3;
end
% in randi(a,b,c) always a>>b .
Solution:
matrix=trio(2,4)
matrix =
1 1 1 1
1 1 1 1
2 2 2 2
2 2 2 2
3 3 3 3
3 3 3 3

Nisheeth Ranjan on 28 May 2020
function [T] = trio(n,m)
T1 = ones(n,m);
T2 = 2*T1;
T3 = 3*T1;
T = [T1;T2;T3];
end

Ashitha Nair on 15 Jun 2020
function T=trio(n,m)
t=3*n;
x=(2/3)*t;
A=ones(t,m);
y=x-(n-1);
z=t-(n-1);
A([y:x],:)=2;
A([z:t],:)=3;
T=A;
end
This is how I solved it.

Angshuman Thakuria on 18 Jun 2020
function T = trio(n,m)
T = randi(2,3*n,m);
T([((3*n/3-(n-1))):1*3*n/3],:) = 1;
T([((2*3*n/3-(n-1))):2*3*n/3],:) = 2;
T([((3*3*n/3-(n-1))):3*3*n/3],:) = 3;
end

youssef boudhaouia on 25 Jul 2020 at 12:02
Edited: youssef boudhaouia on 25 Jul 2020 at 12:03
Check here a simple solution in only one line!
function T=trio(n,m)
T=[ones(n , m) ; 2 * ones(n , m) ; 3 * ones(n , m) ];
end