Create a function that uses indexing

Given: Create a function called pick_username that will receive an input argument, which is a vector of values. The function will randomly pick one element out of the provided input vector vec and return the value in that element. Remember, your element numbers are integers from 1 to N, where N is the total number of values in the vector.
Find: How do you select a single element number that is a random integer from 1 to N? How do you then take that random element number and return Value as the actual number saved in that random location in vector vec?
Issue: I'm not sure if I am supposed to be using a function that ramdomizes results or just using the randi function.
My Solution: Does this make sense?
%% Thanks to Victor, I think I understand what to do here, I just want
%% to make sure it makes sense to people with vastly more experience in MATLAB environment than myself.
function Value=pick_kweave19(vec)
%length(vec); % This function is used to determine the length of vector,
%but do I even need this line of code if I have the next line as is?
%random_index = randi(length(vec)); % I took this line of code out as it is
%redundant, I assigned Value to the output instead
Value=randi(length(vec))
end
% Code used to call function vec=1:1:100; Value=pick_kweave19(vec)

5 comentarios

Dyuman Joshi
Dyuman Joshi el 7 de Mzo. de 2024
"% but how do you select a single element number that is a random integer from 1 to N?
Refer to the documentation of randi. Whenver you have any question(s) about a function, it is always best to refer to its documentation.
% How do you then take that random element number and return Value as the actual number saved in that
% random location in vector vec?"
Use indexing.
I recommend you take the free MATLAB Onramp tutorial to learn the essentials and syntax of MATLAB.
Spaceman
Spaceman el 8 de Mzo. de 2024
Thanks, I have very limited time to actually troubleshoot code, but I do think about it all day at my high tempo job. The codes I am trying to troubleshoot come from S. Attaway, Matlab: a practical introduction to programming and problem solving. Butterworth-Heinemann, 2019. I will try to run through the MATLAB Onramp tutorial on my day off this week.
Dyuman Joshi
Dyuman Joshi el 8 de Mzo. de 2024
Editada: Dyuman Joshi el 8 de Mzo. de 2024
"My Solution: Does this make sense?"
It does,
"%% I just want to make sure it makes sense to people with vastly more experience in MATLAB environment than myself."
but (imo) you should not worry about this. You should focus on whether you know and understand what's going on.
Also, try incorporating comments in your code, so that you have an idea what each line does. And it will be helpful to others and to you for future reference.
In any case, (if ever in doubt) you should test your code against multiple inputs, and see if the outputs are as expected or not.
Victor
Victor el 8 de Mzo. de 2024

"%but do I even need this line of code if I have the next line as is?"

It is not necessary, I just put a separated line there so that you can know how to find the length of the input array.

Spaceman
Spaceman el 9 de Mzo. de 2024
Thank you both, I always only come to MATLAB answers after I have been trying different things and staring blanky at my monitor for hours on end with no success. I always try to scrounge from the help menu and documentation as well. It makes sense to me after I know the sensical way it needs to be done, and from my mistakes I always learn.

Iniciar sesión para comentar.

 Respuesta aceptada

Victor
Victor el 7 de Mzo. de 2024
Editada: Victor el 7 de Mzo. de 2024
Here is the full program, it can run in a m-file, if you want the function only, you can spilt it and use it in other m-file. Remember, return 1 thing at a time and if you have any questions, feel free to ask! :D
namearray = {'apple', 'banana', 'orange', 'grape','mango'}; % Create array with string elements
pick_kweave19(namearray)
function Value=pick_kweave19(array)
length(array); % This function is used to determine the length of array
random_index = randi(length(array)) % Choosing a random index in the length of array, add ' ; ' at the
% end if you dont want it to appear on command window
Value=random_index;
%Value=array(random_index); % Uncomment this line if you wish to return
% the value of array at the random index
% Remeber to return 1 thing only at a time!!
end

4 comentarios

Spaceman
Spaceman el 8 de Mzo. de 2024
Editada: Spaceman el 8 de Mzo. de 2024
Genius! Thank you for your response, Victor. I can kind of see the logic in your function... You create a char array and call it namearray. (But why did you create it outside of the function?). The next line of code is the title of the function (as a function of the namearray? What does this line do?)... You create a function with output argument of Value, function name of pick_kweave19, and input argument of (array). You then use the length function on (array) to determine the length, or max size. random_index is the variable that returns a random integer, (index?), of the previous variable. Then Value=random_index is essentially stating the output of the variable so the function can run it. I think I understand. Where did we define the variable array we are referencing? Is that the name array? Would this code work if instead of a character array I used an input vector, vec? Thank you in advance. I am just trying to learn freeform.
Victor
Victor el 8 de Mzo. de 2024
Editada: Victor el 8 de Mzo. de 2024
-I create the array outside of the function because of your request: "Create a function called pick_username that will receive an input argument, which is a vector of values". You can put that array inside the function, obviously, no problem at all :D
-The next line: pick_kweave19(namearray), is to call the function. The function is just a set of many commands, so if you want to run the commands in the function, just call its name. The part inside () is the function's parameter. This function has a parameter called "array" (you created a function with a parameter above). This can be understood as the input to the function. So the line pick_kweave19(namearray) is like input the parameter array you declare above into the function. There is no ';' at the end of the line because I want to display the result to the command window.
-I can see that the part you explain the operation of the function is correct. Well done!
- The variable array is declared at the top of the program, which is used as input to the function (mentioned above).
-When you input a character array to the function above, for example 'hello', you can imagine that length() will be the length of the array, which is 5, you can try that out, but I think in your case, you should input name array. If you change the names in that array in to character, like name={'a', 'b','c'}; it would work perfectly just fine like the above program.
Just change whatever you want to customise it to serve your purpose. You can really learn alot by trying! If you want to know more, just ask me :D
Victor
Victor el 8 de Mzo. de 2024
You can accept my answer so that if anyone encounters the question like you, they can read my explanation :D
Spaceman
Spaceman el 9 de Mzo. de 2024
Editada: Spaceman el 9 de Mzo. de 2024
I will be happy to accept your answer, as it helped me to get done what I believe the exercise was asking. ONE more question, is the function, length, commonly used when indexing? It was weird because when I learned it I wondered why someone would need to know the max value of a particular array/vector. It seems it's often used when trying to do what we did above by selecting a random integer from a vector of integers... I just don't know/understand why yet... Also is it possible to return MORE than one value when indexing? E.g. 2 instead of 1 like above?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos

Versión

R2023b

Etiquetas

Preguntada:

el 7 de Mzo. de 2024

Editada:

el 9 de Mzo. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by