create a function that takes a vector as an input parameter and returns another vector where each component is the initial vector repeated n times.

create a function that takes a vector as an input parameter and returns another vector where each component is the initial vector repeated n times.
I have thought about doing the following:
function B = cyclic_shift_recursion(A,n) %n is the number of times the vector is repeated and A is the vector that I want to repeat
if n<=0
return
else
B=[A cyclic_shift_recursion(A,n-1)];
end
end
I get an error and I do not understand why, how can I correct this? Is there another easy way to do this? Thank you.

 채택된 답변

Guillaume
Guillaume 2019년 3월 21일
편집: Guillaume 2019년 3월 21일
"Output argument "B" (and maybe others) not assigned during call to "cyclic_shift_recursion"
Well, yes matlab is correct. Your code:
function B = cyclic_shift_recursion(A,n) %n is the number of times the vector is repeated and A is the vector that I want to repeat
if n<=0
return
%... rest doesn't matter
So for n = 0, the function returns immediately, without assigning anything to B.
Simply assign something to B before returning. As it's homework, I'm not going to tell you what to assign, but it's very trivial.
As to your question:
" But how can I do a function that does this without using repmat"
There's all sort of ways you could cheat, avoiding repmat while not adhering to the spirit of your assignment (which it seems is to use recursion). E.g.:
kron(ones(1, n), A)
reshape(repelem(A(:),1, n), 1, [])
A(reshape((1:numel(A))' + zeros(1, n), 1, []))
and probably more...

댓글 수: 6

Yes, I've already realized what was missing and I could correct it, this is the final program:
function B = cyclic_shift_recursion(A,n)
if n<=0
B=A;
return
else
B=[A cyclic_shift_recursion(A,n-1)];
end
end
Thank you very much for the help.
I don't think it's A you should return when n is 0.
Have you tried your code? Hasn't the result for one more repetition than it should?
I already tried it and it works, it does not repeat more than the account.
Maybe, I misunderstood the assignment, but I would think that
cyclic_shift_recursion([1 2 3], 1)
should return [1 2 3], not [1 2 3 1 2 3]. That's the way repmat works.

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

추가 답변 (1개)

Nathali - what is the error message? What is cyclic_shift_recursion? Consider using repmat since your homework is indicating that you need to repeat each component in the initial array n times...

댓글 수: 1

The error message is that:
Output argument "B" (and maybe others) not assigned during call to "cyclic_shift_recursion".
cyclic_shift_recursion It's the name of my function that has a recursive form.
Using repmat is very easy, because what I need would be repmat(A,1,n). But how can I do a function that does this without using repmat? .Many thanks for your help.

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2019년 3월 21일

댓글:

2019년 3월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by