Info

This question is locked. 編集または回答するには再度開いてください。

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

24 ビュー (過去 30 日間)
Marlon Izaguirre
Marlon Izaguirre 2019 年 5 月 1 日
Locked: Rik 2024 年 7 月 9 日
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.
  6 件のコメント
mahmoud khaled
mahmoud khaled 2020 年 8 月 11 日
function T=trio(n,m)
T=[ones(n,m) ; (2*ones(n,m)) ; (3*ones(n,m))];
end
Rik
Rik 2020 年 8 月 17 日
編集済み: Rik 2020 年 8 月 18 日
I'm going to delete duplicate answers. I will consider the list below as the list of solutions. From the current answer section I will only leave the top one for each of these:
  1. all answer with general advice about how to solve the question
  2. answers with more than 1 vote
  3. allocating the full size array with ones or zeros and indexing into it, writing the correct values
  4. allocating the full size array with a non-standard function (like randi) and indexing into it, writing the correct values
  5. 3 and 4, but for the three parts separately, requiring concatenation
  6. kron (posted in a comment)
  7. repmat combined with implicit expansion
If new valid solutions are posted I will of course leave those as well, although I think the non-esoteric solutions may be exhausted.

採用された回答

James Tursa
James Tursa 2019 年 5 月 1 日
編集済み: James Tursa 2019 年 5 月 1 日
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)
  3 件のコメント
Ajay Raineni
Ajay Raineni 2020 年 4 月 4 日
Pls can you help me
Ammar
Ammar 2023 年 9 月 27 日
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

その他の回答 (6 件)

AYUSH GURTU
AYUSH GURTU 2019 年 5 月 28 日
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
  2 件のコメント
sona rai
sona rai 2020 年 8 月 9 日
% 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
SATHISHKUMAR S
SATHISHKUMAR S 2021 年 8 月 3 日
What is the input argument for this?

PRAKASH ANAND
PRAKASH ANAND 2019 年 11 月 8 日
% 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
  7 件のコメント
Shubham
Shubham 2021 年 12 月 31 日
Thank you brother
OUSSAMA El GABBARI
OUSSAMA El GABBARI 2022 年 1 月 19 日
that function you made I see it's very restricted.. I wonder if it'd work for matrices of larger dimensions !

evan muas
evan muas 2019 年 12 月 2 日
function T=trio(n,m)
T=[ones(n,m);2*ones(n,m);3*ones(n,m)]
end
  3 件のコメント
Upoma Saha
Upoma Saha 2020 年 7 月 19 日
this is abosulely amazing.
Akinola Tomiwa
Akinola Tomiwa 2020 年 7 月 25 日
Nice

Juvraj Singh Sandhu
Juvraj Singh Sandhu 2020 年 10 月 4 日
編集済み: Juvraj Singh Sandhu 2020 年 10 月 4 日
this will work for all inputs
function T= trio(n,m);
T1= ones(n,m);
T2= 2*ones(n,m);
T3= 3*ones(n,m);
T= [T1;T2;T3];
  2 件のコメント
Hashir
Hashir 2023 年 9 月 8 日
good try, but i dont think function will return 3n by m matrix.
Stephen23
Stephen23 2023 年 9 月 8 日
"but i dont think function will return 3n by m matrix."
Please show any positive integer values of m & n for which this function does not return a 3n by m matrix.

mayank ghugretkar
mayank ghugretkar 2019 年 6 月 5 日
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 件のコメント
SWAMI NAIDU BODDU
SWAMI NAIDU BODDU 2020 年 6 月 1 日
Perfect code for random inputs also...... Thanks you sirr!!.....

Doga Savas
Doga Savas 2019 年 8 月 22 日
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
  3 件のコメント
Rik
Rik 2023 年 9 月 8 日
trio_even_less_baloney = @(n,m) repelem((1:3).',n,m);
n=2;m=4;
trio_even_less_baloney(n,m)
ans = 6×4
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3
Although your point is absolutely valid.
DGM
DGM 2023 年 9 月 8 日
Yeah, maybe I should've called it trio_reduced_baloney()
I figured repelem() was already officially exhausted. Reshaping/permuting might not be minimal, but it was a reasonable basic option that was still fair game.

This question is locked.

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by