compare using if and or condition

1 visualización (últimos 30 días)
MatLab Code N
MatLab Code N el 17 de Jun. de 2019
Comentada: MatLab Code N el 20 de Jun. de 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 comentarios
Geoff Hayes
Geoff Hayes el 17 de Jun. de 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
MatLab Code N el 17 de Jun. de 2019
Here is the part of the code:
[num1, txt1, raw1]=xlsread('sample_data.xlsx','T_a');
[num2, txt2, raw2]=xlsread('sample_data.xlsx','T_b');
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!

Iniciar sesión para comentar.

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 18 de Jun. de 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.
  1 comentario
MatLab Code N
MatLab Code N el 20 de Jun. de 2019
I just made a small modification in the code and it does the work. Thanks for your help,
[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) || 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

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by