Matlab, mathematical algorithm

In summary, the conversation is about writing an algorithm for a class that involves using a random number generator to pick from 3 objects. The speaker suggests assigning the objects to different ranges of numbers, but gets stuck when considering what to do if the generator produces a 0. They consider using a function to run the random generator again, but are unsure if it is the best solution. One suggestion is to add 1 to the generator's output, but the speaker is unsure if this will work in MATLAB.
  • #1
sapiental
118
0
Hey there,

I was just wondering if anyone in here could help me out with a short algorithm I have to write for my class.

Lets say the function fix(10*rand(1,1)) gives u a random number out of (0,1,2,3,4,5,6,7,8,9)

now, you need to use the number from the generator to pick out of 3 objects (A,B,C)

i.e pick = fix(10*rand(1,1))

What I'm thinking is to assign A = [1,2,3] B = [4,5,6] C = [7,8,9]

but where I get stuck is what if the random generator gives me a 0?

Could I just write another function saying that if pick = 0 then to run the random function again? But I feel that this would not make the program truly dependant on rand(1,1) which we are instructed to use. But then there is no way to assing 3 objects to 10 numbers??

Any suggestions are much appreciated. Thank You
 
Physics news on Phys.org
  • #2
Add 1 to 9*rand(1,1)? My only concern is MATLAB is a nightmare to use (syntactically) so I'm not sure if the answer is that simple.
 
  • #3
Try using
pick = ceil(3*rand)
 

1. What is Matlab?

Matlab is a high-level programming language and interactive environment designed specifically for numerical computation and data visualization. It is widely used in various scientific and engineering fields for data analysis, modeling, and simulation.

2. What is a mathematical algorithm?

A mathematical algorithm is a set of step-by-step instructions or procedures used to solve a specific problem or perform a specific task. It is a series of mathematical operations and calculations that are designed to produce a desired output or result.

3. How do I write and run a mathematical algorithm in Matlab?

To write and run a mathematical algorithm in Matlab, you first need to define the variables and their values, then use built-in functions and operators to perform the mathematical operations. Finally, you can use the "run" or "evaluate" command to execute the algorithm and view the output.

4. Can Matlab be used for machine learning and artificial intelligence?

Yes, Matlab has a variety of built-in functions and toolboxes that can be used for machine learning and artificial intelligence applications. It also has a user-friendly interface and powerful visualization tools that make it a popular choice for data scientists and researchers.

5. How do I troubleshoot errors in my Matlab algorithm?

If your Matlab algorithm is not running correctly or producing unexpected results, you can use the "debugging" feature to identify and fix any errors. This allows you to step through the code and track the values of variables to pinpoint where the problem is occurring.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • Set Theory, Logic, Probability, Statistics
Replies
11
Views
390
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
Back
Top