Check whether values of a certain column of a matrix is between two values

조회 수: 2 (최근 30일)
%{
I have a BUS matrix with 6 rows, column 12 is the max value and column 13 is the minimum value.
I want to check if column 8 values are between column 12 and 13 values.
Now, I know that the answer of volmax are all greater than 0 but some values of volmin are less than 0.
For some reason, my code display Yes which means that volmax and volmin are greater than or equal to zero.
But I know this is not true. My guess is that my code is comparing only the first row and it ignore the rest of the rows
Can you help me to figure this out?
}%
volmax= BUS(:,12)- BUS(:,8) % we are good if volmax >=0
volmin= BUS(:,8)- BUS(:,13) % we are good if volmin >=0
if volmax<0 | volmin <0
disp('NO')
else
disp('YES')
end
end

채택된 답변

the cyclist
the cyclist 2021년 11월 30일
OK, still trying to understand everything, but I think this does what you want:
% Sample input
BUS(:, 8) = [2;2;2];
BUS(:,12) = [3;3;3];
BUS(:,13) = [1;1;1];
volmax = BUS(:,12)- BUS(:,8); % we are good if volmax >=0
volmin = BUS(:,8)- BUS(:,13); % we are good if volmin >=0
if any((volmin<0)|(volmax<0))
disp('NO')
else
disp('YES')
end
Change the inputs around, and see if it is correct. If this is NOT correct, then please
  • give an example of inputs that give INCORRECT output, and explain why
  댓글 수: 2
Steven Shaaya
Steven Shaaya 2021년 11월 30일
Thank you very much, yes it did work.
Can you help me with something else, it is related to this question
Now in my if statement im using a matrix with 6 rows, is there away that I can use | with another matrix that has only 3 rows.
for example
if any((volmin<0)|(volmax<0) |(H<0)) %where H is a matrix with 3 rows
%where volmin and/or volmax has 6 rows
disp('NO')
else
disp('YES')
end
% it gives the following error, "Matrix dimensions must agree."
Steven Shaaya
Steven Shaaya 2021년 11월 30일
Thank you again. I figured out the answer to my question which is adding zeros to the matrix in order to make them of the same size.
If you have better method, I would like to hear from you.

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

추가 답변 (1개)

the cyclist
the cyclist 2021년 11월 30일
I think you intended
volmax<0 & volmin <0
rather than
volmax<0 | volmin <0
because you want to meet both conditions ("AND"), not just either condition ("OR").
Also, be aware that for vector input V, you will only enter the if statement
if V
...
end
if all elements of the vector V are true. (It is not some kind of implicit loop that works down the vector.)
Note that the expression
volmax<0 | volmin <0
(not using the if structure at all) will be a logical vector, with TRUE/FALSE entries corresponding to each row.
  댓글 수: 4
the cyclist
the cyclist 2021년 11월 30일
I misunderstood your logic, and I see why you chose | now. Sorry for the confusion.
In your original code, it is not just checking the first element; it is checking whether ALL the elements of the vector meet the condition. Here is a simple example:
if [true true true]
disp('All elements are true')
else
disp('At least one element is false')
end
All elements are true
if [true false true]
disp('All elements are true')
else
disp('At least one element is false')
end
At least one element is false
The reason your second code example works is that you are using a for loop, checking one value at a time (not trying to check the entire vector at once).
Question: Suppose 2 rows do not meet the condition, and 3 rows do. What do you want for output? Should the output be 5 "YES" / "NO" values (one per row), or do you just want a single "YES" / "NO", based on whether all rows are ok?
Steven Shaaya
Steven Shaaya 2021년 11월 30일
편집: Steven Shaaya 2021년 11월 30일
I want just a single "YES" / "NO", based on whether all rows are ok, all values of column 8 should be between the values of columns 12 & 13.
So based on your assumption, it should display 'no'.
For example, I can try
if volmax(1:6,:) <0 | volmax(2:6,:) <0 |volmax(3:6,:) ...........ect
but I do not see this efficient because I will need about 12 OR only for this example and my next example will have about 30 OR
that why I am trying to find another method
Thank you

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by