compare using if and or condition
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
MatLab Code N
il 17 Giu 2019
Commentato: MatLab Code N
il 20 Giu 2019
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 Commenti
Geoff Hayes
il 17 Giu 2019
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%)?
Risposta accettata
Geoff Hayes
il 18 Giu 2019
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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Spreadsheets in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!