How to write function for above combine matrix?

A = [2,4]
A = [1 1 1 1 ; 1 1 1 1; 2 2 2 2; 2 2 2 2; 3 3 3 3; 3 3 3 3]
what will be the funtion for A= [7,4]
or using any random variable?
A should give (3n by m) matrix

15 comentarios

Walter Roberson
Walter Roberson el 2 de Jun. de 2019
See repelem() and repmat()
dpb
dpb el 2 de Jun. de 2019
And possibly kron()
Adam Danz
Adam Danz el 5 de Jun. de 2019
What would A[7,3] look like?
Walter Roberson
Walter Roberson el 6 de Jun. de 2019
Editada: Walter Roberson el 14 de Jun. de 2020
I do not understand why 7 3 would result in single rows of 1 and 11, and not two rows of 1 with no 11.
Walter Roberson
Walter Roberson el 6 de Jun. de 2019
Editada: Walter Roberson el 6 de Jun. de 2019
RAHUL ANTIL comments to Adam Danz
A[7,3] A = [ 1 1 1
2 2 2
2 2 2
3 3 3
3 3 3
4 4 4
4 4 4
5 5 5
5 5 5
6 6 6
6 6 6
7 7 7
7 7 7
8 8 8
8 8 8
9 9 9
9 9 9
10 10 10
10 10 10
11 11 11 ]
Walter Roberson
Walter Roberson el 6 de Jun. de 2019
RAHUL ANTIL comments to me:
sorry its 2 rows of 1 and single row of 11
Walter Roberson
Walter Roberson el 6 de Jun. de 2019
Editada: dpb el 23 de Sept. de 2019
RAHUL, please do not use "flag" unless you need to alter [sic. "alert" is what was intended I think (dpb] the moderators about something.
To respond to us, click on the "Comment on this Question" link just below this box.
A(n,m) gives (3n,m) matix
A[7,3] A = [ 1 1 1
1 1 1
2 2 2
2 2 2
3 3 3
3 3 3
4 4 4
4 4 4
5 5 5
5 5 5
6 6 6
6 6 6
7 7 7
7 7 7
8 8 8
8 8 8
9 9 9
9 9 9
10 10 10
10 10 10
11 11 11 ]
Adam Danz
Adam Danz el 6 de Jun. de 2019
Sorry, my pattern recognition skills require more time than I'd like to spend trying to figure out the relationship between the input and output. Can you explain the pattern in words?
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. For an example,see the code 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
[My function generate correct pattern when (n) is even.]
Stephen23
Stephen23 el 6 de Jun. de 2019
@RAHUL ANTIL: please show us the function you wrote.
function T = trio(n,m)
n = 3*n;
w = (n)/2;
y = ceil(w);
A = [1:y]';
B = ones(2,m);
T = kron(A,B);
Adam Danz
Adam Danz el 6 de Jun. de 2019
ahhhh... there we go. That's more valuable than any example or description.
Adam Danz
Adam Danz el 6 de Jun. de 2019
NOTE: The code provided above by OP is apparently incorrect and the pattern is still unexplained.
This is what I came up with ...Hope this helps!!!
function T= trio(n,m);
n=3*n;
T1= ones(n/3,m);
T2= 2*ones(n/3,m);
T3= 3*ones(n/3,m);
T= [T1;T2;T3];

Iniciar sesión para comentar.

Respuestas (7)

saurav Tiwari
saurav Tiwari el 13 de Jun. de 2020
function T=trio(n,m)
a=ones(n,m);b=2*a;c=3*a;
T=[a;b;c];
end

4 comentarios

saurav Tiwari
saurav Tiwari el 13 de Jun. de 2020
most easiest coding of the world
Adam Danz
Adam Danz el 14 de Jun. de 2020
This does not produced the expected pattern described by OP.
Chandan Kumar
Chandan Kumar el 23 de Feb. de 2021
function T=trio(n,m)
a=ones(n,m);b=2.*a;c=3.*a;
T=[a;b;c];
he should have added dot to perform array multiplication.
Walter Roberson
Walter Roberson el 23 de Feb. de 2021
When at least one of the operands is a scalar then the * operator does element by element multiplication and acts like .*

Iniciar sesión para comentar.

Adam Danz
Adam Danz el 6 de Jun. de 2019
Editada: Adam Danz el 6 de Jun. de 2019
n = 7;
m = 3;
A = repelem((1:ceil((3*n)/2))',2,m);

9 comentarios

RAHUL ANTIL
RAHUL ANTIL el 6 de Jun. de 2019
@Adam Danz
Variable A should be size of [21,3] but the output is [22,3].
Adam Danz
Adam Danz el 6 de Jun. de 2019
Editada: Adam Danz el 6 de Jun. de 2019
It's producing the exact same output as your function which, given those inputs, produces a 22x3 matrix.
T = trio(7,3);
>> size(T)
ans =
22 3
RAHUL ANTIL
RAHUL ANTIL el 6 de Jun. de 2019
The function returns a 3n-by-m matrix called T.
I was getting the same result before.
Adam Danz
Adam Danz el 6 de Jun. de 2019
Editada: Adam Danz el 6 de Jun. de 2019
Your function and the code in my answer produce the exact same results
% YOUR CODE
n = 7;
m = 3;
% T = trio(n,m)
n = 3*n;
w = (n)/2;
y = ceil(w);
A = [1:y]';
B = ones(2,m);
T = kron(A,B);
% MY CODE
n = 7;
m = 3;
A = repelem((1:ceil((3*n)/2))',2,m);
% COMPARE THE RESULTS
isequal(A,T)
ans =
logical
1 % <---- exactly the same results
Run that code and look at your T and my A. They are exactly the same.
RAHUL ANTIL
RAHUL ANTIL el 6 de Jun. de 2019
Our code print the same result. But my point is that when n=7;m=3, than output matrix size should be [21,3].
our code print the matrix size of [22,3]. That's wrong
Adam Danz
Adam Danz el 6 de Jun. de 2019
How can anyone here know what your code is supposed to do?
RAHUL ANTIL
RAHUL ANTIL el 6 de Jun. de 2019
My code is wrong I know that and that's why I'm here seeking for help.
Adam Danz
Adam Danz el 6 de Jun. de 2019
Ok. What patterns are you looking for? Can you explain it in words?
Walter Roberson
Walter Roberson el 6 de Jun. de 2019
Change Adam's ceil() to floor(). Then at the end if size(A,1) is not a multiple of 3, append 3-mod(number_of_rows,3) copies of a row with the next integer.

Iniciar sesión para comentar.

RAHUL ANTIL
RAHUL ANTIL el 6 de Jun. de 2019
T = [7,3]
T =
[ 1 1 1
1 1 1
2 2 2
2 2 2
3 3 3
3 3 3
4 4 4
4 4 4
5 5 5
5 5 5
6 6 6
6 6 6
7 7 7
7 7 7
8 8 8
8 8 8
9 9 9
9 9 9
10 10 10
10 10 10
11 11 11]
I want above pattern, matrix size is [21,3].

3 comentarios

Adam Danz
Adam Danz el 6 de Jun. de 2019
What rules did you follow to build this matrix?
saurav Tiwari
saurav Tiwari el 13 de Jun. de 2020
uhhh combination rule of matrix
dpb
dpb el 13 de Jun. de 2020
Uhhhh...which combinaton "rule" is that?
You have N copies of two but only one for the last...whassup w/ that?

Iniciar sesión para comentar.

AN Roza
AN Roza el 23 de Abr. de 2020
V. Given
3157926
5502222
221810.58
7925651
5511591
1172396
00.50.370.20.10.9
x











Write the answer of the following comment:
a. x(3,5)
b. x(7,7)
c. x(1:5, :)
d. x(1:end, :)

1 comentario

Walter Roberson
Walter Roberson el 23 de Abr. de 2020
no, this is a completely incorrect answer to the question asked by RAHUL.

Iniciar sesión para comentar.

dpb
dpb el 13 de Jun. de 2020
Editada: dpb el 14 de Jun. de 2020
" want above pattern, matrix size is [21,3]."
ADDENDUM/ERRATUM:
[But we have now learned that that is NOT even the correct description of the assignment -- there is no "odd man out" in the desired result, the output is always a multiple of 3x the requested N so there is never an odd set.]
Original solution moved to end solves the problem as first described simply to reproduce a given stated output; will leave but the problem described is solved as
function T=trio(n,m)
% build 3*n x m output matrix with thirds of vectors of 1, 2, 3, respectively.
T=kron([1:3].',ones(n,m));
end
I would suggest NOT turning in the above Answer to a homework assignment, however -- is NOT going to pass the smell test of something most beginning students will have come up with on own.
"Just sayin'..." :)
END ADDENDUM/ERRATUM --
ORIGINAL ANSWER FOLLOWS...BUT ANSWERS DIFFERENT QUESTION/PATTERN
R=21;C=3;N=2; % define parameters, R, C, repeat count
P=kron([1:ceil(R/N)].',ones(N,C)); % pattern complete N*ceil(R/N) rows
P=P(1:R,:); % fixup for odd man out if mod(R,N)~-0
results in
>> P=kron([1:ceil(R/N)].',ones(N,C)); P=P(1:R,:)
P =
1 1 1
1 1 1
2 2 2
2 2 2
3 3 3
3 3 3
4 4 4
4 4 4
5 5 5
5 5 5
6 6 6
6 6 6
7 7 7
7 7 7
8 8 8
8 8 8
9 9 9
9 9 9
10 10 10
10 10 10
11 11 11
K>>
Cyrus David Pastelero
Cyrus David Pastelero el 22 de Jun. de 2020

0 votos

function T = trio(n,m)
T1 = ones(n,m); //this just creates the body for us to use, you can also use zeros
T = [T1;T1+1;T1+2]; //adding them base on the required output
end

1 comentario

Adam Danz
Adam Danz el 22 de Jun. de 2020
Editada: Adam Danz el 22 de Jun. de 2020
This doesn't even come close to producing the desired outputs.
To come closer, you would need a 3rd input to indicate the number of replications and then you could use that 3rd input to produce T instead of manually concatenating replications of T1.
But even then the function wouldn't be doing what the OP is asking for.

Iniciar sesión para comentar.

Ibad Ur Rahman
Ibad Ur Rahman el 30 de Mzo. de 2022
I got the following answer to this question.....
function T=trio(n,m)
a=ones(n,m);
b=2*ones(n,m);
c=3*ones(n,m);
T=cat(1,a,b,c);
end

Categorías

Preguntada:

el 2 de Jun. de 2019

Respondida:

el 30 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by