Clear Filters
Clear Filters

V = [7 9 -8 9 3 -8 -5 1 10 10 0 -7]; Write a script file that will determine how many times the values increase in V when going from one entry to the next.

2 views (last 30 days)
Write a script file that will determine how many times the values increase in V when going from one entry to the next. For example, when going from V(1) to V(2) the values increase from 7 to 9 so this would be counted as an increase. The script should also count how many times the values decrease and how many times they do not change. I submitted the assignment without answering this question. I used the little MatLab knowledge I have, but could not figure out how to answer this particular question. The teacher's instructions were to not use the sum, min, nor max functions for the assignment. I was able to do it for finding the sum of the negatives and positives, displaying the minimum and maximum numbers, and prompting a user to enter a value to check for inequalities against V. I apologize if my questioning came of as rude and impolite. I just want to understand MatLab better.
clear; clc;
V = [7 9 -8 9 3 -8 -5 1 10 10 0 -7];
count = 0;
counts = 0;
counte = 0;
for k = 1:length(V)
if V(1:k-1)> V(2:k)
count = count + 1;
end
if V(1:k-1)< V(2:k)
counts = counts + 1;
end
if V(1:k-1) == V(2:k)
counte = counte + 1;
end
end
fprintf( 'The number of times an increase occurs within the vector: %i \n',count);
fprintf( 'The number of times an decrease occurs within the vector: %i \n',counts);
fprintf( 'The number of times no change occurs within the vector: %i \n',counte);
  6 Comments
Jan
Jan on 29 Mar 2017
@Joshua: Thanks for your explanations. Unfortuantely we have too many questions about homework from users who do not care about questions for clarifications. We do not want to support cheating also. But specific questions about Matlab are welcome.
Did you try anything? Then please post it, even if it fails.
Jan
Jan on 29 Mar 2017
@Joshua: +1, Well done. You kave asked a clear question and have solved the problem by your own with the assistance of the forum. This was not a "do-it-4-me" question. Please do not take it personally, that many users of the forum (like me) are spiky when homework questions are asked. When the authors show their own effort, questions are welcome - to be exact: they are the purpose of this forum :-)

Sign in to comment.

Accepted Answer

Jan
Jan on 29 Mar 2017
V = [7 9 -8 9 3 -8 -5 1 10 10 0 -7];
To determine hwo many time the values goes up or down, you need two counters:
goesUp = 0;
goesDown = 0;
Now you have to step through the elements and compare the current element with the previous one. This cannot work for the first element, so start with the 2nd:
for k = 2:length(V)
disp(V(k))
end
Comparing with the previous element works like: V(k) < V(k - 1)
Now you can insert two IF commands using the corresponding conditions and increase the value of the concerned counter.
Instead of the IF command, you can use the fact, that the logical comparison replies TRUE and FALSE, which is converted to 1 or 0 automatically in arithmetic expressions:
x = 0;
x = x + (10 > 9)
x = x + (10 > 12)
Now try it and post your code in case of troubles.

More Answers (2)

Josua Mensah
Josua Mensah on 29 Mar 2017
Edited: Jan on 29 Mar 2017
clear; clc;
V = [7 9 -8 9 3 -8 -5 1 10 10 0 -7];
count = 0;
counts = 0;
counte = 0;
for k = 2:length(V)
if V(k) > V(k-1)
count = count + 1;
end
if V(k) < V(k-1)
counts = counts + 1;
end
if V(k) == V(k-1)
counte = counte + 1;
end
end
fprintf('The number of times an increase occurs within the vector: %i \n',count);
fprintf('The number of times an decrease occurs within the vector: %i \n',counts);
fprintf('The number of times no change occurs within the vector: %i \n',counte);
[JS, code formatted]

Rik
Rik on 29 Mar 2017
We are not paid consultants, so don't give orders. This is the place where you can find help if you get stuck, not the place where you can find people that will do your homework for you.
However, I'm a sucker, so here are two lines of code that should help you.
a=sum(diff(v)==0);
b=sum(diff(v)>0);
  1 Comment
Josua Mensah
Josua Mensah on 29 Mar 2017
I submitted the assignment without answering this question. I used the little MatLab knowledge I have, but could not figure out how to answer this particular question. The teacher's instructions were to not use the sum, min, nor max functions for the assignment. I was able to do it for finding the sum of the negatives and positives, displaying the minimum and maximum numbers, and prompting a user to enter a value to check for inequalities against V. I apologize if my questioning came of as rude and impolite. I just want to understand MatLab better.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!