Shifting an array without circshift
17 visualizaciones (últimos 30 días)
Matt J el 11 de Mayo de 2023
Editada: Matt J el 11 de Mayo de 2023
circshift is not doing anymore than indexing into the array like below, so you could do that directly.
xshift = x([end,1:end-1])
However, as @DGM points out, there's no way to know what advice will avoid the errors you see, without knowing what you did to invoke them.
John D'Errico el 11 de Mayo de 2023
Editada: John D'Errico el 11 de Mayo de 2023
Nobody can know what you did wrong. circshift DOES work. I'm not even sure what you would have done that would cause an error. The tool is pretty simple to use. By error, do you just mean you did not get what you wanted? But since we can't possibly know what you wanted, we cannot possibly help you. Would you ask your auto mechanic to diagnose what is wrong with your car, while not actually allowing the mechanic to touch or look at the car, and only saying that it does not work poperly?
The answer to that is NOT to then ask if someone teach you how to build your own car from scratch. It is far better to learn to use and understand the tools you have.
Can you circularly shift an array? Well, of course. That is trivial. Just use mod.
A = primes(20)
I'll give an example of how it might work. Think about it, then look at the code.
nA = numel(A);
ind = 0:nA-1; % zero based index
A(mod(ind - 1,nA)+1) % The same as circshift(A,1)
So, we can now see how one might build a simple function to circularly shift a vector.
mycircshift = @(A,shift) A(1 + mod((0:numel(A)-1) - shift,numel(A)));
The fact is though, you would be better off just using circshift. What I wrote is not any better. If has less functionality than circshift. And if using circshift gave you a problem, then I am sure you would have a problem debugging what you did wrong in the code I just wrote.
So unless you need a code to do something more than what circshift provides, then you should just learn to properly use circshift. And if you do need something more sophisticated, then you need to explain what circshift fails to do for you.