Learn How to Perfectly Shuffle Vectors in Matlab with Simple Pseudo-code"

In summary, the person is having trouble understanding how to do a perfect shuffle of a vector. They are supposed to split the vector in half then the 1st element is the 1st element in the first half and the 2nd element is the 1st element of the 2nd half, and so on.
  • #1
phillyj
30
0
I'm having trouble understanding how to do a perfect shuffle of a vector. I'm supposed to split the vector in half then the 1st element is the 1st element in the first half and the 2nd element is the 1st element of the 2nd half, and so on.

For example, if I have [1 2 3 4 5 6]
then I need to rearrange it into [1 4 2 5 3 6]

Can someone at least help me with the pseudo-code? I been thinking about this a few days with no luck. I am only allowed a few functions like "find" or "length". Nothing complex.
 
Physics news on Phys.org
  • #2
bah, messed up. I figured this out a couple months ago. Will repost once I confirm.
 
Last edited:
  • #3
%it has to be an even length vector, but:


N = length(x)/2
y = zeros(N,1)

for n = 1:N

y(2*n-1) = n
y(2*n) = N+n
end
 
  • #4
Can you do this without using for loops? We haven''t covered that yet. I may have to ask the professor. Its surprising because even the TA had no clue how to do this with the material we have covered so far.

Thanks
 
  • #5
Wow, I can't thing of how you do this without a loop unless you exacty knew your input.

If I have any insights, I'll share them with you.
 
  • #6
I like pythag's answer better, but something like this would work.

A=[1,2,3,4,5,6]
B=reshape(A,[3,2])
C=reshape(B',[1,6])

Probably other ways of doing it using the reshapes and row/column swaps, etc.
 
  • #7
Pythagorean said:
%it has to be an even length vector, but:


N = length(x)/2
y = zeros(N,1)

for n = 1:N

y(2*n-1) = n
y(2*n) = N+n
end

Can you explain this to me since I never used loops before? Also, any particular reason why you specify Nx1 zero vector rather than 1xN vector?
 
  • #8
phillyj said:
Can you explain this to me since I never used loops before? Also, any particular reason why you specify Nx1 zero vector rather than 1xN vector?

A for loop just goes through all the lines between FOR and END, changing the specified value every time, so you can base your code on that evolving number.

1xN or Nx1 is arbitrary in general. You may have a specific preference depending on the rest of your code. They're equivalent though in the absence of conflicting comparisons.
 
  • #9
Hi,

your problem can be solved by this way

%let c be the vector which has to be rearranged
% assuming size of c to be even
c=1:10;

% d is the vector where we store the rearranged vector

d = zeros(size(c));
d(1:2:end) = c(1:end/2);
d(2:2:end) = c(end/2+1,end);

%thats all you need to do ;)

Hope it will help!

Chhitiz
http://www.chhitizbuchasia.com"
 
Last edited by a moderator:

1. How do I start learning how to shuffle vectors in Matlab?

To start learning how to shuffle vectors in Matlab, you can first familiarize yourself with the basic syntax and functions of the language. Then, you can try out some simple examples of shuffling vectors using pseudo-code. This will help you understand the concept and get comfortable with the process.

2. What is the purpose of shuffling vectors in Matlab?

The purpose of shuffling vectors in Matlab is to randomize the elements within a vector. This can be useful in various applications such as data analysis, machine learning, and simulation. Shuffling vectors can also be used in games or randomizing processes.

3. Can I shuffle multiple vectors at the same time?

Yes, you can shuffle multiple vectors at the same time in Matlab. This can be done by using the same pseudo-code for each vector or by creating a loop that shuffles each vector separately. The important thing is to ensure that the same shuffling process is applied to all the vectors.

4. Is there a specific function for shuffling vectors in Matlab?

There is no specific function for shuffling vectors in Matlab, but there are various built-in functions that can be used for this purpose. Some examples include the "randperm" function, which generates a random permutation of integers, and the "shuffle" function, which randomly rearranges the elements of a vector.

5. Can I shuffle vectors with different lengths?

Yes, you can shuffle vectors with different lengths in Matlab. However, you may need to take extra steps to ensure that the shuffling process is fair and that all elements in each vector have an equal chance of being shuffled. This can be done by using the "randperm" function to generate random indices for each vector and then shuffling the elements according to those indices.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
115
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top