display value if it lies between x and y

조회 수: 4 (최근 30일)
C.G.
C.G. 2021년 1월 18일
댓글: C.G. 2021년 1월 18일
I have a series of csv files, which I want to input into a for loop, which tells me if any of the numbers in column 5 of the csv are between -0.16 and -0.13.
Currently when I run this i get the error: " Operands to the || and && operators must be convertible to logical scalar values."
Could anybody help me please?
ymax = -0.13;
ymin = -0.16;
for b = 1:length(particledata)
%save all the rows in the 6th column (x-coordinates) of each cell as a new variable x
x{b} = particledata{b}(:,5);
%use the function table2array to turn the format of the data from a table to an array
x_array{b} = table2array(x{b});
%for all the rows in the array, if the x coordinate goes beyond the outlet
%of the rice pile, display 'grain left rice pile'
if (x_array{b}>ymin && x_array{b}<ymax)
disp('grain left rice pile');
end
%sum the total number of grains leaving the rice pile in each cell, and save into a new variable 'grains'
grains{b}=sum(x_array{b}>ymin) && (x_array{b}<ymax)
fprintf('A total of %d grains left the rice pile\n',grains{b});
end

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 1월 18일
편집: KALYAN ACHARJYA 2021년 1월 18일
Try with single "&"
Note "x_array{b}>ymin" or "x_array{b}<ymax" return logical array data. If all the array elements of the array is greater than ymin or vice versa, it generates array with all logical ones, other case array with zeros
The if condition is true ...only if
x_array{b}>ymin retun 1 1 1 1....and
x_array{b}<ymax return 1 1 1 1.....
  댓글 수: 1
C.G.
C.G. 2021년 1월 18일
I have tried with a single '&' and the error still occurs.
I need the code to identify all the cells that contain a number between -0.16 and -0.13 as i want to save these numbers into a new csv.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by