display value if it lies between x and y
3 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (1 件)
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.....
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!