How do I refer to only the odd-numbered elements in any given vector?
Given a vector I want to write a function that only refers to the odd-numbered elements in that given vector. How would I do this?
1 Comment
Stephen23 on 3 Feb 2015
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
What is an "odd-numbered" element? An element for which the index is odd (keep in mind that MATLAB uses one-based indexing!), or where the element value itself is odd?
Answers (2)
Star Strider on 3 Feb 2015
Direct link to this answer
Cancel Copy to Clipboard
Direct link to this answer
Cancel Copy to Clipboard
I’m not certain what you mean by ‘odd-numbered elements’, so here are two possibilities:
oddidx = @(v) v(1:2:end); % Addressing Odd-Indexed Elements
oddval = @(v) v(rem(v,2) == 1); % Addressing Odd-Valued Elements
y1 = oddidx(v)
y2 = oddval(v)
10 12 14 16 18 20
11 13 15 17 19
0 Comments
MD ZIHADUL ISLAM TUSAR on 3 Oct 2022
Direct link to this answer
Cancel Copy to Clipboard
Direct link to this answer
Cancel Copy to Clipboard
function y = everyOther(x)
0 Comments
See Also
Categories
Find more on Just for fun in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Australia (English)
- India (English)
- New Zealand (English)
- 中国