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

  • Context: MATLAB 
  • Thread starter Thread starter phillyj
  • Start date Start date
  • Tags Tags
    Matlab Vectors
Click For Summary

Discussion Overview

The discussion centers around how to perform a perfect shuffle of a vector in Matlab, specifically focusing on splitting the vector in half and interleaving the elements. Participants are seeking pseudo-code and alternative methods to achieve this without using complex functions or loops.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant describes the desired output of shuffling a vector, providing an example with specific elements.
  • Another participant mentions they previously solved the problem but has not confirmed the solution yet.
  • A proposed solution involves using a for loop to rearrange the vector, but it is noted that the vector must be of even length.
  • One participant asks if the task can be accomplished without using for loops, expressing surprise at the lack of clarity even among teaching assistants.
  • Another participant expresses doubt about achieving the shuffle without loops unless the input is known in advance.
  • A different approach is suggested using the reshape function in Matlab, which involves manipulating the dimensions of the vector.
  • Clarifications are requested regarding the use of loops and the choice between different vector orientations (1xN vs Nx1).
  • A participant provides a solution using indexing to rearrange the vector without loops, assuming the vector size is even.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method to shuffle the vector, with multiple competing approaches and uncertainty about the use of loops. Some participants express confusion about the implementation details.

Contextual Notes

There are limitations regarding the assumptions about the input vector's length and the functions allowed for use. The discussion reflects varying levels of familiarity with Matlab programming concepts among participants.

Who May Find This Useful

This discussion may be useful for students learning Matlab, particularly those interested in vector manipulation and programming techniques without advanced functions.

phillyj
Messages
30
Reaction score
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
bah, messed up. I figured this out a couple months ago. Will repost once I confirm.
 
Last edited:
%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 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
 
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.
 
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.
 
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?
 
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.
 
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:

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K