926 views (last 30 days)

This question is soft-locked: new answers that are equivalent to already posted answers may be deleted without prior notice.

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

sona rai
on 9 Aug 2020

% 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

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

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

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

Opportunities for recent engineering grads.

Apply TodayFind the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 6 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

## 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_951952

⋮## 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_951952

## 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_968209

⋮## 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_968209

## 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_976104

⋮## 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_976104

Sign in to comment.