Ask your own question, for FREE!
Mathematics 21 Online
OpenStudy (raffle_snaffle):

help with matlab plzzzzz

OpenStudy (raffle_snaffle):

%% 5.23 % The histc function can be used to determine the number of A's, B's, % C's, D's, and F's in the grade data from problem 5.21, if the % following grading scheme applies % A > 90 to 100 % B > 80 to 90 % C > 70 to 80 % D > 60 to 70 % F > 0 to 60 % Use the bar function to create a histogram based on this scheme. G = [68, 83, 61, 70, 75, 82, 57, 5, 76, 85, 62, 71, 96, 78, 76, 68, 72, 75, 83, 93]; % final test scores newG = sort(G(:)); % final scores, starts with lowest then end with highest [rrA, ccA] = find((newG < 100) & (newG > 90)); A = newG(rrA); [rrB, ccB] = find((newG < 90) & (newG > 80)); B = newG(rrB); [rrC, ccC] = find((newG < 80) & (newG > 70)); C = newG(rrC); [rrD, ccD] = find((newG < 70) & (newG > 60)); D = newG(rrD); [rrF, ccF] = find(newG < 60); F = newG(rrF);

OpenStudy (raffle_snaffle):

I am just trying to learn for myself how to make a 20x3 vector

OpenStudy (raffle_snaffle):

I think I would need to use an if else statement, but how do I solve the problem?

OpenStudy (raffle_snaffle):

@Loser66

OpenStudy (raffle_snaffle):

%% 5.23 % The histc function can be used to determine the number of A's, B's, % C's, D's, and F's in the grade data from problem 5.21, if the % following grading scheme applies % A > 90 to 100 % B > 80 to 90 % C > 70 to 80 % D > 60 to 70 % F > 0 to 60 % Use the bar function to create a histogram based on this scheme. G = [68, 83, 61, 70, 75, 82, 57, 5, 76, 85, 62, 71, 96, 78, 76, 68, 72, 75, 83, 93]; % final test scores newG = sort(G(:)); % final scores, starts with lowest then end with highest A = find((newG < 100) & (newG > 90)); bar(A) B = find((newG < 90) & (newG > 80)); bar(B) C = find((newG < 80) & (newG > 70)); bar(C) D = find((newG < 70) & (newG > 60)); bar(D) F = find((newG < 60) & (newG > 50)); bar(F)

OpenStudy (loser66):

hahaha.. wrong person!! Sorry friend. I don't know.

OpenStudy (raffle_snaffle):

@jim_thompson5910

OpenStudy (raffle_snaffle):

remember the lab assignment you helped me with. I seem to have lost the program =( I have no clue where it is... ugh.

OpenStudy (raffle_snaffle):

anyhow we can talk about that later. What about the question above?

jimthompson5910 (jim_thompson5910):

which lab? and I'm still reading over and thinking of this code above

OpenStudy (raffle_snaffle):

ahhhh i found it!!!!!

OpenStudy (raffle_snaffle):

whew, I thought i lost it...

OpenStudy (raffle_snaffle):

For the code above I was thinking about using an if else statement. However, I have not learned if else statements in my class. I don't think we learn that until chapter 6 or 7. We are currently in chapter 5.

jimthompson5910 (jim_thompson5910):

one sec, trying something out

OpenStudy (raffle_snaffle):

kk

jimthompson5910 (jim_thompson5910):

ok I think I found what to do. You're on the right track. You just need to now extract the number of rows of each matrix then use those values to form a new matrix which will help form the bar graph one sec while I write up that bit of code

OpenStudy (raffle_snaffle):

wait hold on.

OpenStudy (raffle_snaffle):

Basically I was trying to extract the indices or at least replace the indices with the correct letter grade. I was using [rrA, A] = find((newG > 90) & (newG < 100)); Why doesn't this spit out the grade score corresponding to the letter grade?

jimthompson5910 (jim_thompson5910):

when you wrote [rrA, ccA] = find((newG < 100) & (newG > 90)); you didn't need to have 2 outputs like that. newG is a vector so you don't need to worry about the second dimension ---------------- you can write this rrA = find((newG < 100) & (newG > 90)); and be just fine. The same applies to the other similar lines of code.

OpenStudy (raffle_snaffle):

okay so then how to you get the letter grade to correspond with the score? Do you have to use another command to do this?

OpenStudy (raffle_snaffle):

or do you take two commands and integrate them together as one?

jimthompson5910 (jim_thompson5910):

`Basically I was trying to extract the indices or at least replace the indices with the correct letter grade. I was using [rrA, A] = find((newG > 90) & (newG < 100)); Why doesn't this spit out the grade score corresponding to the letter grade?` the `find` function goes through a vector and it looks for values based on the conditions you gave it. In this case, you told matlab to look through newG and pull out values between 90 and 100 (exclude the endpoints). The find function simply returns the indices. Basically the addresses of where the values are located. It does NOT return the actual values. That's why you had to tack on `A = newG(rrA);` to actually pull out those values (in this case, 93 and 96)

OpenStudy (raffle_snaffle):

yes I agree with the above statement. Go ahead and write the small bit of code up. We can then discuss it.

jimthompson5910 (jim_thompson5910):

ok this should work. See the attached m file

jimthompson5910 (jim_thompson5910):

oops I have it in reverse order. For some reason I told myself to reverse m1 through m5. My bad. I fixed it

OpenStudy (raffle_snaffle):

oh nice, all I needed was to use the size() command? I don't need the cc in each set of brackets? rrA = find((newG < 100) & (newG > 90)); A = newG(rrA) rrB = find((newG < 90) & (newG > 80)); B = newG(rrB) rrC = find((newG < 80) & (newG > 70)); C = newG(rrC) rrD= find((newG < 70) & (newG > 60)); D = newG(rrD) rrF = find((newG < 60) & (newG > 0)); F = newG(rrF)

jimthompson5910 (jim_thompson5910):

`I don't need the cc in each set of brackets?` no because matrix newG is really a vector. When you use the find function on a vector, it will spit out just 1 output value: the index or indices where the values are located

OpenStudy (raffle_snaffle):

it spits out M = the value and I = indice? This would be two output technically?

jimthompson5910 (jim_thompson5910):

I guess if you really want to be technical, newG is a 20x1 matrix when you say [rrA, ccA] = find( ... blah blah stuff dealing with newG .... ) it will return different values for rrA, but ccA will always be equal to 1 because there's only one column here. So ccA isn't needed

OpenStudy (raffle_snaffle):

^ okay thanks for that detailed statement. It makes sense. Let me finish putting the rest of the code in the script and playing around with it a bit.

jimthompson5910 (jim_thompson5910):

http://www.mathworks.com/help/matlab/ref/find.html if you want the values, then you have to say `[row,col,v] = find(___) ` that description given on the link says... `"also returns vector v, which contains the nonzero elements of X."` though when I test it, it doesn't seem to work. So I'd just do it the way we're already doing it

OpenStudy (raffle_snaffle):

newG = 5 57 61 62 68 68 70 71 72 75 75 76 76 78 82 83 83 85 93 96

OpenStudy (raffle_snaffle):

here is newG so when I run [m1, n1] = size(A) it outputs m1 = 2 n1 = 1 m1 = row2 and n1 = col1 of newG ?

jimthompson5910 (jim_thompson5910):

no, matrix is being used here A = 93 96 matrix A has 2 rows, 1 column

OpenStudy (raffle_snaffle):

ahhhhh okay I see

jimthompson5910 (jim_thompson5910):

matrix A is what you set up: the matrix of scores that correspond to A grades

jimthompson5910 (jim_thompson5910):

now I noticed that you don't have any "or equal to" as part of any of your inequality signs. So values like 70 are not part of any bars at all. This score is completely excluded which isn't what you want

jimthompson5910 (jim_thompson5910):

I guess the only issue is the score 70 so that's the good news

OpenStudy (raffle_snaffle):

So the size() command puts the numbers in ascending order. [m1,m2,m3,m4] = size(X) returns m1 = 2, m2 = 3, m3 = 4, m4 = 1 [m,n] = size(X) returns m = 2, n = 12

OpenStudy (raffle_snaffle):

I read your statement and thinking.

jimthompson5910 (jim_thompson5910):

let me think of over `[m1,m2,m3,m4] = size(X)`

jimthompson5910 (jim_thompson5910):

uh oh, now you have overlap

OpenStudy (raffle_snaffle):

basically what you are saying is if you don't include the equal to or less than sign you will not output 70 because it doesn't include 70 if we only use <

OpenStudy (raffle_snaffle):

you are right overlap. let me fix this

jimthompson5910 (jim_thompson5910):

consider this bit of code rrA = find((newG <= 100) & (newG >= 90)); A = newG(rrA); rrB = find((newG <= 90) & (newG >= 80)); B = newG(rrB); notice how 90 is going to be in matrix AND in matrix B

OpenStudy (raffle_snaffle):

G = [68, 83, 61, 70, 75, 82, 57, 5, 76, 85, 62, 71, 96, 78, 76, 68, 72, 75, 83, 93]; % final test scores newG = sort(G(:)); % final scores, starts with lowest then end with highest rrA = find((newG <= 100) & (newG > 90)); A = newG(rrA); rrB = find((newG <= 90) & (newG > 80)); B = newG(rrB); rrC = find((newG <= 80) & (newG > 70)); C = newG(rrC); rrD= find((newG <= 70) & (newG > 60)); D = newG(rrD); rrF = find(newG <= 60); F = newG(rrF); % get the sizes of each matrix [m1, n1] = size(A) [m2, n2] = size(B) [m3, n3] = size(C) [m4, n4] = size(D) [m5, n5] = size(F)

jimthompson5910 (jim_thompson5910):

much better, then you'll end up with this histogram

jimthompson5910 (jim_thompson5910):

before, it was perfectly symmetrical. Now it has 1 more score added to the D bar (going from height of 4 to 5)

jimthompson5910 (jim_thompson5910):

oh btw, but don't forget to put `clc; clear all;` at the top. This should be done at the top of any script so you always start over fresh each time you run the script.

OpenStudy (raffle_snaffle):

y = [m1, m2, m3, m4, m5] y = 2 4 7 5 2

jimthompson5910 (jim_thompson5910):

looks good, those y values are the heights of the bars

OpenStudy (raffle_snaffle):

yes, but does the 2 represents the size(A)

OpenStudy (raffle_snaffle):

Size(A) = 2x1 matrix because we only had two scores in between 90 and 100

jimthompson5910 (jim_thompson5910):

it represents the number of rows in A, so yes in a way

jimthompson5910 (jim_thompson5910):

btw I realized that you could do m1 = size(A,1) to get the number of rows

jimthompson5910 (jim_thompson5910):

size(A,1) returns the number of rows in A size(A,2) returns the number of columns in A

jimthompson5910 (jim_thompson5910):

`Size(A) = 2x1 matrix because we only had two scores in between 90 and 100` yes correct

OpenStudy (raffle_snaffle):

awesome and my code works. I am just glad I understand what is going on. Thanks for your help. I need to work on my lab. If I have anymore questions I will make a new thread.

OpenStudy (raffle_snaffle):

actually can you take a look at my last problem? I finished it just want to make sure my logic is correct

OpenStudy (raffle_snaffle):

%% 5.25 % Use the randn function to create 1000 values in a normal (Gaussian) % distribution of numbers with a mean of 70 and a standard deviation % of 3.5. Create a histogram of the data set you calculated. % given information n = 1000; mu = 70; S = 3.5; R = normrnd(70, 3.5, 1000, 1); % normal Gaussian nbins = 15; % number of bins hist(R, nbins);

jimthompson5910 (jim_thompson5910):

why did you set up n,mu,S when you didn't use them?

jimthompson5910 (jim_thompson5910):

you used hard coded values instead

OpenStudy (raffle_snaffle):

because I wasn't thinking...

jimthompson5910 (jim_thompson5910):

I also notice it says to use the randn function but you wrote normrnd is there a difference? hmm let me research

OpenStudy (raffle_snaffle):

I found a example using normrnd() instead of randn()

jimthompson5910 (jim_thompson5910):

https://answers.yahoo.com/question/index?qid=20100608095023AAaafrT the page says `"Documentation states that normrnd uses the randn function"` strange how that works lol

OpenStudy (raffle_snaffle):

I searched "normal Gaussian" and found on mathworks site normrnd. So it used it because it worked.

jimthompson5910 (jim_thompson5910):

ok normrnd it is

OpenStudy (raffle_snaffle):

My instructor has us putting cell blocks on one script (%%). I have 8 to 10 problems on one script separated in cells. Is there a way to only run one cell at a time? What I have been doing is suppressing each cell before running the code of interest.

OpenStudy (raffle_snaffle):

I suppress by using '%' on each line of code

jimthompson5910 (jim_thompson5910):

you can run the whole file or you can run one particular cell only

jimthompson5910 (jim_thompson5910):

you don't have to suppress with comments

OpenStudy (raffle_snaffle):

I meant suppress the code not the comments

jimthompson5910 (jim_thompson5910):

how are you running the script? are you pushing a button or hitting a key on the keyboard?

OpenStudy (raffle_snaffle):

I click the little green arrow on the editor tab called 'run'

jimthompson5910 (jim_thompson5910):

http://www.mathworks.com/help/matlab/matlab_prog/run-sections-of-programs.html go to the part `Run the code in the current section.`

jimthompson5910 (jim_thompson5910):

by 'section' they mean 'cell' (I think anyway)

OpenStudy (raffle_snaffle):

yep you are right it works. I just have to click in the cell. it turns yellow showing me I am in the cell.

jimthompson5910 (jim_thompson5910):

yeah its wherever the blinking text cursor is

OpenStudy (raffle_snaffle):

I was probably confusing myself because I left old output in the command window which might have made me think it was running more than one mini program.

jimthompson5910 (jim_thompson5910):

yeah which is why clc is a good idea. But I'm not sure whether to put it at the top of each cell or put it at the top of the entire script file

jimthompson5910 (jim_thompson5910):

it happens to me a lot too

OpenStudy (raffle_snaffle):

so should I put clc; clear in each cell? Make this a habit? Instructor never advised us to put these commands in each tab

OpenStudy (raffle_snaffle):

cell* not tab

jimthompson5910 (jim_thompson5910):

Again I'm not sure. If one cell depends on the other, then 'clear all' is a bad idea to put at the top of each cell. If you delete data set up in cell A, and cell B needs it, then you'll run into errors or get weird results but if each cell is independent and separate from one another, then `clc;clear all` at the top of each cell is probably not a bad idea.

OpenStudy (raffle_snaffle):

let me try it

jimthompson5910 (jim_thompson5910):

I guess clc isn't as bad because yes the command window is cleared out, but at least the data variables are kept intact

jimthompson5910 (jim_thompson5910):

If cell B depends on cell A, I would save the data needed for cell B in a separate MAT file. That way you can safely clear all the variables out and then load in whatever data you need for cell B If you do that then you can have `clc; clear all;` at the top of each cell and have no issues if certain cells depend on other cells

OpenStudy (raffle_snaffle):

clear all clears what again? 'clear' clears the command window.

OpenStudy (raffle_snaffle):

So running clc; clear all will erase whatever output is located in the command window. It won't clear what is the workspace

jimthompson5910 (jim_thompson5910):

http://www.mathworks.com/help/matlab/ref/clc.html clc clears the command window `clc clears all input and output from the Command Window display, giving you a "clean screen."`

OpenStudy (raffle_snaffle):

maybe not use clear all but clear instead?

jimthompson5910 (jim_thompson5910):

The `clear` function removes variables from the workspace `clear all` will get rid of EVERYTHING in the workspace regardless of what type of data type it is apparently, and I'm just learning this, saying `clear` is the same as `clear all` though I haven't fully tested this http://www.mathworks.com/help/matlab/ref/clear.html

OpenStudy (raffle_snaffle):

it won't clear anything in the work space when using clear all

jimthompson5910 (jim_thompson5910):

`clear removes all variables from the current workspace, releasing them from system memory.`

jimthompson5910 (jim_thompson5910):

clear all should empty the entire workspace

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!