2,073 views (last 30 days)

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.

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)

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

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

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

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

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 !

Nijita Kesavan Namboothiri
on 25 Jun 2019

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 at 2:18

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👍

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

Saradha Manikandan
on 19 May 2020

Kulasekaran N
on 12 Jun 2020 at 17:57

function T = trio(n,m)

T = [ones(n,m);2*ones(n,m);3*ones(n,m)];

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

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

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 at 10:59

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 at 15:35

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 at 9:20

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

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/459693-how-to-replace-elements-in-top-third-middle-third-and-bottom-third-of-matix#comment_859948

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/459693-how-to-replace-elements-in-top-third-middle-third-and-bottom-third-of-matix#comment_859948

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/459693-how-to-replace-elements-in-top-third-middle-third-and-bottom-third-of-matix#comment_902193

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/459693-how-to-replace-elements-in-top-third-middle-third-and-bottom-third-of-matix#comment_902193

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/459693-how-to-replace-elements-in-top-third-middle-third-and-bottom-third-of-matix#comment_902199

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/459693-how-to-replace-elements-in-top-third-middle-third-and-bottom-third-of-matix#comment_902199

Sign in to comment.