compare using if and or condition

2 visualizzazioni (ultimi 30 giorni)
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 CommentiMostra NessunoNascondi Nessuno
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%)?
MatLab Code N il 17 Giu 2019
Here is the part of the code:
ncols=size(num1);
AE_Combined=[];
for c = 1:ncols(1,1)
if num2(:,5) > num1(:,5) | num1(:,5) > 0.95 * num2(:,5)
AE_Combined=num2(:,6);
else
AE_Combined=num1(:,6);
end
end
In this code the second section of the or satetment is not working. The first section of the if-condition is checking if the Flow_b is greater than Flow_a, which is working. In the second section, I want to check if the Flow_a is greater than Flow_ b by 5%.
And yes, I want to compare each element of the column.
Thanks!

Accedi per commentare.

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);
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.
1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
MatLab Code N il 20 Giu 2019
I just made a small modification in the code and it does the work. Thanks for your help,
nrows = size(num1, 1 );
AE_Combined = zeros(nrows, 1 );
for r = 1:nrows
if num2(r,5) > num1(r,5) || 0 < (num1(r,5) - num2(r,5)) < (0.05 * num2(r,5))
AE_Combined(r) = num2(r,9);
else
AE_Combined(r) = num1(r,9);
end
end

Accedi per commentare.

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!

Translated by