Creating a vector ranging then iterating?

So I have an assignment:
1. First, create a vector vec, which should include 30 integer numbers ranging from -10 to 25. Then write a simple procedure to iterate the vector vec and do the following: · Add 1 to all even numbers · Add 3 to all odd numbers
2. Convert the operations from the previous step to a function called vecUpdate(), it takes only one vector parameter (i.e., vec), and outputs a vector (vecOut) that shows the updated results from step 1.
I would like some input on how to carry this out, not an answer please. I am completely new to this field and struggling. Help is greatly appreciated for experience.
Thanks, James

Respuestas (2)

Star Strider
Star Strider el 2 de Oct. de 2016
Editada: Star Strider el 2 de Oct. de 2016

0 votos

Since your vector has to be random (your vector has to have 30 elements, not the 36 the colon operator would produce), search the documentation for the function that returns random integers.
After that, think about how you would detect even and odd numbers. What would be the remainder after division? (That hint should also lead you to the function you need.)
Also, see the documentation for Matrix Indexing. You have a vector, not a matrix, so the section on logical indexing will be easy to implement here.
You’ve apparently already had instruction in writing functions, so that part should be straightforward. (If you’ve not, see the documentation on Function Basics.)
EDIT — (02 October 2016 05:30 UCT)
Not necessary to use a loop. Use ‘matrix indexing’ instead.

1 comentario

Image Analyst
Image Analyst el 2 de Oct. de 2016
Depends on what the professor was thinking and expecting when he said that they had to "iterate the vector". If he'll allow internal iterations via vectorized operations, then the whole #1 can be done in 5 lines of code. One to create a vector, one that is the function declarations line, a line to find the even indexes, a line to add one to even indexes, and a line to add 3 to odd indexes.
Maybe he'd get extra credit if he did it both ways, vectorized and a for loop.

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 2 de Oct. de 2016
For #1, use
r = randi([-10, 25],....... etc.
then use a for loop to do the iterations it's asking for. Inside the for loop, use mod() or rem().
for k = 1 : length(vec)
if mod(................
vec(k) = ............
end
Should be easy to finish.
For #2, use the function statement. It should be trivial. Here is the line from the help
function [y1,...,yN] = myfun(x1,...,xM)
so it should be really trivial to adapt that and put your code from #1 inside it.

2 comentarios

James Larsen
James Larsen el 2 de Oct. de 2016
Could you perhaps go fully into detail about the odd/even part?
Did you try mod(vec(k), 2)? It doesn't seem like it. Not sure why you didn't, but here is a little more:
for k = 1 : length(vec)
if mod(vec(k), 2) == 0
% This element is even.
vec(k) = vec............+......;
else
% This element is odd.
vec(k) = vec(...........
end
end
If I do much more, you'll simply be copying my answer.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 1 de Oct. de 2016

Comentada:

el 2 de Oct. de 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by