Repeating elements in a vector
조회 수: 16 (최근 30일)
이전 댓글 표시
How do I write a function with loops that takes a two column matrix and has the element of one column be repeated by the other column? For example,
A=
4 1
5 2
6 3
>>([4,1; 5,2; 6,3])
ans: 4 5 5 6 6 6
댓글 수: 3
Evan
2012년 10월 10일
help repmat
You might find that function to be of use. Also, think about concatenation.
Jan
2012년 10월 10일
Please do not post solutions of homework questions. On one hand the OP cannot submit this without cheating, on the other hand his teacher will not recommend this forum anymore. So the OP's and the forum's reputation suffer.
답변 (3개)
Andrei Bobrov
2012년 10월 10일
편집: Andrei Bobrov
2012년 10월 11일
for solution your problems you can use functions of MATLAB: cumsum; zeros.
댓글 수: 2
Jan
2012년 10월 10일
Please do not post solutions of homewokr questions. On one hand the OP cannot submit this without cheating, on the other hand his teacher will not recommend this forum anymore. So the OP's and the forum's reputation suffer.
Biside this, your soultion is smart and efficient. +1
Evan
2012년 10월 10일
function v = fname(A)
v = [];
for i = 1:size(A,1)
v = [v repmat(A(i,1),1,A(i,2))];
end
댓글 수: 3
Jan
2012년 10월 12일
@Erika: Question, especially from beginners, are very welcome in this forum. Asking a perfect question concerning homework problems is nearly impossible, because this requires a deep understanding of the question, which you reach usually after the problem is solved.
As long as the contributors of this forum answer homework question such general that the pupils/students perform the main part by their own, all kind of questions are welcome and useful.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!