MATLAB Answers

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

1.495 views (last 30 days)
Marlon Izaguirre
Marlon Izaguirre on 1 May 2019
Commented: mahmoud khaled on 11 Aug 2020 at 12:05
My task is the following:
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?
Thank you in advance.

  5 Comments

Show 2 older comments
Sai Swaroop Maram
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
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

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 1 May 2019
Edited: James Tursa on 1 May 2019
Your row indexing is wrong.
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)

More Answers (32)

AYUSH GURTU
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
sona rai on 9 Aug 2020 at 2:41
% sir this is right code instead of your code.
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

Sign in to comment.


PRAKASH ANAND
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

  4 Comments

Show 1 older comment

Sign in to comment.


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

  3 Comments

Stephen Cobeldick
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

Sign in to comment.


mayank ghugretkar
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 !

Doga Savas
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

  0 Comments

Sign in to comment.


Nijita Kesavan Namboothiri
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

  2 Comments

Jayanta Sarkar
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

Correct answer of above question👍

Sign in to comment.


samyak doshi
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

  2 Comments

Saradha Manikandan
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

Sign in to comment.


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

  0 Comments

Sign in to comment.


Bruno Luong
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)

  0 Comments

Sign in to comment.


Yihan Liu
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

  0 Comments

Sign in to comment.


Sowmya Allani
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

  0 Comments

Sign in to comment.


Akash Devalekar
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

  0 Comments

Sign in to comment.


SADIQ GBAGBA
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

  0 Comments

Sign in to comment.


Irfan Hussain
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

  0 Comments

Sign in to comment.


Priya
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

  0 Comments

Sign in to comment.


Vivek Mokal
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

  0 Comments

Sign in to comment.


Siddharth Joshi
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

  0 Comments

Sign in to comment.


Vishal Natraj
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

  0 Comments

Sign in to comment.


Ali Badr
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

  0 Comments

Sign in to comment.


Arooba Ijaz
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

  0 Comments

Sign in to comment.


Deep Raj Bhujel
Deep Raj Bhujel on 6 May 2020
This one will help you.
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

  0 Comments

Sign in to comment.


kannan vidyadhar
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

  0 Comments

Sign in to comment.


ERTIZA HOSSAIN SHOPNIL
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

  0 Comments

Sign in to comment.


Taif Ahmed BIpul
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

  2 Comments

Qaiser khan Bozdar
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
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.

Sign in to comment.


Rahmi Elagib
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];

  0 Comments

Sign in to comment.


MD.Ismail Emon
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

  0 Comments

Sign in to comment.


muyiwabowen
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

  0 Comments

Sign in to comment.


AYUSH MISHRA
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

  0 Comments

Sign in to comment.


Nisheeth Ranjan
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

  0 Comments

Sign in to comment.


Ashitha Nair
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.

  0 Comments

Sign in to comment.


Angshuman Thakuria
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

  0 Comments

Sign in to comment.


youssef boudhaouia
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

  0 Comments

Sign in to comment.


Translated by