how to replace elements in top third, middle third, and bottom third of matix
Infos
This question is locked. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
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 commentaires
Gowtham Karaka
le 24 Mai 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
MD. AL NAHIAN SAYEM
le 17 Juin 2020
Modifié(e) : Walter Roberson
le 13 Oct 2021
function k=trio(a,b)
x=ones(a,b);
y=2*ones(a,b);
z=3*ones(a,b);
k=[x y z]
end
MD. AL NAHIAN SAYEM
le 17 Juin 2020
For random input use k=trio(round(rand),round(rand))
Sai Swaroop Maram
le 28 Juil 2020
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
le 11 Août 2020
function T=trio(n,m)
T=[ones(n,m) ; (2*ones(n,m)) ; (3*ones(n,m))];
end
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:
- all answer with general advice about how to solve the question
- answers with more than 1 vote
- allocating the full size array with ones or zeros and indexing into it, writing the correct values
- allocating the full size array with a non-standard function (like randi) and indexing into it, writing the correct values
- 3 and 4, but for the three parts separately, requiring concatenation
- kron (posted in a comment)
- 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.
Réponse acceptée
Plus de réponses (6)
AYUSH GURTU
le 28 Mai 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
2 commentaires
sona rai
le 9 Août 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
SATHISHKUMAR S
le 3 Août 2021
What is the input argument for this?
PRAKASH ANAND
le 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
7 commentaires
Risa Dwi Ratnasari
le 8 Déc 2019
hello.... why you write
x=ones?
Danish Dutt Attrey
le 28 Mai 2020
To get a matrix with all the numbers which are one
Rafael Eduardo Pachas Vega
le 4 Août 2020
awesome
vanam sindhuja
le 10 Août 2020
It's so easy and presence of mind,smart thinking.Thank you
SUMAN CHATTERJEE
le 6 Sep 2020
Shubham
le 31 Déc 2021
Thank you brother
OUSSAMA El GABBARI
le 19 Jan 2022
that function you made I see it's very restricted.. I wonder if it'd work for matrices of larger dimensions !
evan muas
le 2 Déc 2019
function T=trio(n,m)
T=[ones(n,m);2*ones(n,m);3*ones(n,m)]
end
3 commentaires
Stephen23
le 17 Avr 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
Upoma Saha
le 19 Juil 2020
this is abosulely amazing.
Akinola Tomiwa
le 25 Juil 2020
Nice
Juvraj Singh Sandhu
le 4 Oct 2020
Modifié(e) : Juvraj Singh Sandhu
le 4 Oct 2020
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];
mayank ghugretkar
le 5 Juin 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 !
1 commentaire
SWAMI NAIDU BODDU
le 1 Juin 2020
Perfect code for random inputs also...... Thanks you sirr!!.....
Doga Savas
le 22 Août 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
3 commentaires
Sure, but why use rand()/randi() to simply preallocate an array? Why would people upvote it?
I mean, if the name of the game is embellishment, you could always go further and make it obvious.
trio(2,4)
function d = trio(n,m)
a = imread('peppers.png');
t = 1:size(a,3)-1;
a = imgaussfilt(a,mean(t));
x = mod(double(a),permute(size(a,t(1))-(2298/6:-1:2667/7),[1 3 2]));
q = max(x,[],ones(1,2)+[0 1]) + norm(ones(1,size(a,3)))/(2*cosd(30));
c = reshape(q,1,[]).*ones(n*m,round(pi));
d = permute(reshape(c,m,[]),[fliplr(t) sum(t)]);
end
... but that's a lot of work for nothing.
function d = trio_no_baloney(n,m)
d = ones(n*m,3).*(1:3);
d = reshape(d,m,[]).';
end
trio_even_less_baloney = @(n,m) repelem((1:3).',n,m);
n=2;m=4;
trio_even_less_baloney(n,m)
Although your point is absolutely valid.
DGM
le 8 Sep 2023
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.
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!