How to use for and if statements together?
Mostra commenti meno recenti
I have this problem in my textbook (MATLAB for Behavioral Scientists by Rosenbaum,Vaughan and Wyble) as I am brand new to MATLAB!
% 5.9.4
% Find out how long it takes your computer to identify values greater
% than the overall mean in a 1000×1000 matrix of random numbers,
% using for and if statements. Also find out how long it takes your
% computer to identify values greater than the overall mean through
% instant if-ing. Because the matrix is large, you will want to suppress
% most other output.
I can't seem to workout how to integrate the 'identify larger than overall mean part' of the loop.
Risposta accettata
Più risposte (2)
Noah Prisament
il 29 Feb 2024
2 voti
So right now you are trying to solve "identify values greater than the overall mean."
To break up that part of the problem a bit more, lets think about how you would do that as a human. You would start by calculating the mean and then you would compare values against this mean in order to determine if they were greater.
Lets break this up a bit further into steps:
- Calculate the mean of the matrix values
- Iterate through each value in the matrix
- Compare the value at each iteration to the pre-calculated mean
- Do something with the result of this comparison
Steps 1 and 4 can be broken up a bit further, but I will leave that to you to figure out. Also, I'm not sure if you have been taught it yet, but there is some in-built functionality in MATLAB that could assist with step 1 without requiring further breaking it down.
I hope this helps clarify how to approach this problem and gives some insight into future programming problem solving. Remember that you can always think through how you would do something manually and what the steps would be to perform that solution.
John D'Errico
il 29 Feb 2024
Modificato: John D'Errico
il 29 Feb 2024
0 voti
Can you compute the overall mean? Surely you know how to use the function mean. If not, can you compute the overall mean? How would you do that? (MAKE AN EFFORT. Show some effort, and you will get better help.)
Next, can you compare a number to the overall mean? Can you use an if statement? Can you compare the entire matrix to the overall mean?
Being new to MATLAB does not mean you can just give up.
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!