MATLAB, separate odds and evens from matrix

  • Thread starter Thread starter Feodalherren
  • Start date Start date
  • Tags Tags
    Matlab Matrix
Click For Summary

Discussion Overview

The discussion centers around a homework problem involving the separation of odd and even indexed elements from a row vector in MATLAB. Participants explore various methods to achieve this task, considering the implications of using different functions and indexing techniques.

Discussion Character

  • Homework-related

Main Points Raised

  • One participant presents an initial attempt using the mod function to identify odd and even indices but encounters an error when trying to use logical indexing with it.
  • Another participant questions the appropriateness of the mod function for the task, suggesting that the goal is to split the matrix based on index positions rather than values.
  • There is a proposal to use specific index arrays (O for odd indices and E for even indices) to directly access the elements of the matrix.
  • Participants discuss the need for a solution that works for arbitrary length matrices, with suggestions to utilize the colon operator to generate sequences of indices.
  • One participant suggests using the length function to determine the size of the input matrix for dynamic indexing.

Areas of Agreement / Disagreement

Participants generally agree on the need to separate elements based on their indices rather than their values, but there is no consensus on the best method to implement this, with multiple approaches being discussed.

Contextual Notes

Some limitations include the need for clarity on how to handle matrices of varying lengths and the potential confusion surrounding the use of logical indexing versus direct indexing with integer arrays.

Who May Find This Useful

Readers interested in MATLAB programming, particularly those working on array manipulation and indexing techniques, may find this discussion beneficial.

Feodalherren
Messages
604
Reaction score
6

Homework Statement



Given variable b that contains a row vector of length 10, assign the contents of the odd indeces in b to variable odd , and assign the contents of the even indeces of b to variable even.

Examples:

Input b = [8 -3 0 7 -6 -3 1 8 7 -2]
Output odd = [8 0 -6 1 7]
even =[-3 7 -3 8 -2]

Homework Equations

The Attempt at a Solution



b is some arbitrary matrix

O = mod(b,2);
odd = b(O)E = ~mod(b,2);
even = b(E)

What's weird is that it works for b(E) but b(O) yields and error: Subscript indices must either be real positive integers or logicals.
 
Physics news on Phys.org
I am not sure that mod is the best command to use for this purpose. You are just looking to split the matrix into 2 parts based on whether or not the index is odd or ever, right?
You essentially want something that looks like:
b = [8 -3 0 7 -6 -3 1 8 7 -2];
O=[1,3,5,7,9];
E=[2,4,6,8,10];
odd = b(O)
even=b(E)
The mod function was returning a logical index of whether or not the value was odd or even.
 
But it has to be true for any arbitrary matrix of any length of numbers :/.
 
Feodalherren said:
But it has to be true for any arbitrary matrix of any length of numbers :/.
You want to generate the sequences 1,3,5,... and 2,4,6,... to use as indices for your matrix. Do you know of any easy way to do that in MATLAB? Hint: The colon operator.
 
  • Like
Likes   Reactions: Feodalherren
For arbitrary length arrays, you can call length(b) to get your ending value. Or as milesyoung said, it might be more straightforward to use odd = b([1:2:end]) to count by twos using the colon.
 
  • Like
Likes   Reactions: Feodalherren
Ah ok, got it. Thanks!
 

Similar threads

  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K