compare using if and or condition
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have attached a sample excel dataset. In the excel sheet there are two individual sheets 'T_a' and 'T_b'. I want to have a condition which compares the fifth columns of the sheets (Flow_a in sheet T_a and Flow_b in sheet T_b). If the Flow_b is greater than Flow_a ''OR'' if the Flow_a is gretaer than Flow_b by just 5%, then the new variable AE_compared would fill the respective cell with the value in AE_b from sheet T_b, else fill with the value from respective cell in AE_a from sheet T_a.
Thanks!!!!!
2 个评论
Geoff Hayes
2019-6-17
Please show some of the code that you have written already. Are you able to open and read from the Excel file? Are you comparing each element of the two columns to get a result of whether one is greater than the other (by 5%)?
采纳的回答
Geoff Hayes
2019-6-18
So the second condition is if the Flow_a is gretaer than Flow_b by just 5%. Does this mean that Flow_a cannot be more than 5% greater than Flow_b? So your condition would become
(Flow_a - Flow_b) <= 0.05 * Flow_b
Part of the problem might be that you are including all rows in your conditions rather than the individual row values. This confustion could be in part because of the naming of the variables
ncols=size(num1);
ncols isn't a scalar value indicating the number of columns. It is instead a row vector whose elements contain the length of the corresponding dimension of num1. If you want to iterate over each row of num1, then you would do
nrows = size(num1, 1);
and your code would become
[num1, txt1, raw1] = xlsread('sample_data.xlsx','T_a');
[num2, txt2, raw2] = xlsread('sample_data.xlsx','T_b');
nrows = size(num1, 1);
AE_Combined = zeros(nrows, 1);
for r = 1:nrows
if num2(r,5) > num1(r,5) || (num1(r,5) - num2(r,5)) <= (0.05 * num2(r,5))
AE_Combined(r) = num2(r,6);
else
AE_Combined(r) = num1(r,6);
end
end
Note how we pre-allocate the output array and initialize the appropriate row on each iteration of the loop. Please be aware that when you use the colon : in accessing your elements in an array, you are saying "all". So
num2(:,5)
is all rows in the fifth column of num2...which isn't really what you want since you are using a for loop to iterate over each element.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!