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

Marlon Izaguirre

Marlon Izaguirre (view profile)

さんによって質問されました 2019 年 5 月 1 日

Risa Dwi Ratnasari (view profile)

さんによって コメントされました 2019 年 12 月 8 日 6:26
James Tursa

James Tursa (view profile)

さんの 回答が採用されました
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?

0 件のコメント

サインイン to comment.

9 件の回答

James Tursa (view profile)

on 1 May 2019
Edited by James Tursa

James Tursa (view profile)

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

Marlon Izaguirre (view profile)

on 1 May 2019
Thank you so much James!!!! I was able to figure out the third n rows.

サインイン to comment.

AYUSH GURTU (view profile)

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

0 件のコメント

サインイン to comment.

mayank ghugretkar (view profile)

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 !

0 件のコメント

サインイン to comment.

Nijita Kesavan Namboothiri (view profile)

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

0 件のコメント

サインイン to comment.

Doga Savas (view profile)

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 件のコメント

サインイン to comment.

PRAKASH ANAND (view profile)

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

1 件のコメント

Risa Dwi Ratnasari

Risa Dwi Ratnasari (view profile)

on 8 Dec 2019 at 6:26
hello.... why you write
x=ones?

サインイン to comment.

Yihan Liu (view profile)

on 23 Sep 2019
Edited by Yihan Liu

Yihan Liu (view profile)

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 件のコメント

サインイン to comment.

Sowmya Allani (view profile)

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 件のコメント

サインイン to comment.

evan muas (view profile)

on 2 Dec 2019 at 18:16

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

0 件のコメント

サインイン to comment.

Translated by