I have a set of questions based on a task. Basically one of the questions ask to write a function that counts the number of values that have a greater value of 100 and above, in one of the specfic coloumns of a matrix (provided that you input a matrix).
can i double check this code and if so, is there anything i can do to fix the errors i get when i input a matrix, since its not working when i run. Any tips and advice thank you in advance.
function numbers = PARTAQ1(material)
[m n] = size(material)
index = material(:,1);
ys = material(:,2); %the specfic coloumn of intrest
uts = material(:,3);
ym = material(:,4);
cost = material(:,5);
for i = 1:n
if ys >= 100
numbers = sum(ys >= 100);
disp(numbers);
else
disp(0)
end
end

답변 (1개)

Walter Roberson
Walter Roberson 2020년 4월 7일

0 개 추천

if ys >= 100
ys is a vector. When you compare a vector to a value, the result is a logical vector of results. When you have that inside an if test, if is only considered true if all of the entries are non-zero (true). So if at least one entry inside ys is less than 100, then the test would not succeed.
Question: Why are you testing the same thing every iteration of i?

댓글 수: 6

Aroi Mehu
Aroi Mehu 2020년 4월 8일
okay i undertand what you mean by the comment. I may have overcomplicated things, I just want matlab to basically output how many numbers are greater than 100 in ys. So if there is for example [ 200, 100, 150, 140, 210], i want it to output 3, meaning there is three numbers either equal to or greater to 100. So in this case have a over complicated my function?
Aroi Mehu
Aroi Mehu 2020년 4월 8일
thank for the help
Walter Roberson
Walter Roberson 2020년 4월 8일
When the assignment asked you to use a for loop, the intention was that you would check one element at a time and increment a counter if appropriate.
Your code was not just over-complicated, it was also doing the wrong thing.
Aroi Mehu
Aroi Mehu 2020년 4월 9일
could you provide an example to this as I am not really confident in using for loops
count = 0;
X = randi([0 50], 1, 10)
for K = 1 : 10
if mod(X(K),3) == 2 %some test
count = count + 1;
end
end
Aroi Mehu
Aroi Mehu 2020년 4월 9일
thank you so mcuh this will help

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

질문:

2020년 4월 7일

댓글:

2020년 5월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by