How to write function for above combine matrix?

조회 수: 1 (최근 30일)
RAHUL ANTIL
RAHUL ANTIL 2019년 6월 2일
답변: Ibad Ur Rahman 2022년 3월 30일
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
Adam Danz
Adam Danz 2019년 6월 6일
NOTE: The code provided above by OP is apparently incorrect and the pattern is still unexplained.
vivek jason
vivek jason 2019년 9월 23일
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];

댓글을 달려면 로그인하십시오.

답변 (7개)

saurav Tiwari
saurav Tiwari 2020년 6월 13일
function T=trio(n,m)
a=ones(n,m);b=2*a;c=3*a;
T=[a;b;c];
end
  댓글 수: 4
Chandan Kumar
Chandan Kumar 2021년 2월 23일
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 2021년 2월 23일
When at least one of the operands is a scalar then the * operator does element by element multiplication and acts like .*

댓글을 달려면 로그인하십시오.


Adam Danz
Adam Danz 2019년 6월 6일
편집: Adam Danz 2019년 6월 6일
n = 7;
m = 3;
A = repelem((1:ceil((3*n)/2))',2,m);
  댓글 수: 9
Adam Danz
Adam Danz 2019년 6월 6일
Ok. What patterns are you looking for? Can you explain it in words?
Walter Roberson
Walter Roberson 2019년 6월 6일
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.

댓글을 달려면 로그인하십시오.


RAHUL ANTIL
RAHUL ANTIL 2019년 6월 6일
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
saurav Tiwari
saurav Tiwari 2020년 6월 13일
uhhh combination rule of matrix
dpb
dpb 2020년 6월 13일
Uhhhh...which combinaton "rule" is that?
You have N copies of two but only one for the last...whassup w/ that?

댓글을 달려면 로그인하십시오.


AN Roza
AN Roza 2020년 4월 23일
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
Walter Roberson
Walter Roberson 2020년 4월 23일
no, this is a completely incorrect answer to the question asked by RAHUL.

댓글을 달려면 로그인하십시오.


dpb
dpb 2020년 6월 13일
편집: dpb 2020년 6월 14일
" 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 2020년 6월 22일
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
Adam Danz
Adam Danz 2020년 6월 22일
편집: Adam Danz 2020년 6월 22일
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.

댓글을 달려면 로그인하십시오.


Ibad Ur Rahman
Ibad Ur Rahman 2022년 3월 30일
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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by