readMatrix/for loop issue
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ronan Rafferty
el 7 de Dic. de 2020
Comentada: Harry Laing
el 9 de Dic. de 2020
I'm having issues with reading in data and using the for loop. I need the code to read in 90 rows of 13 different parameters of data from a .csv file and understand that each row is one set of data and assign the correct variable to the correct value so that it can then use this to run the decision tree and give me an outcome for each row.
I am using Matlab R2018b version.
This is the piece of code I am using
myData = readmatrix('BME501_Coursework_Testdata.csv');
for i = 1 : size(myData,1)
thisrow = myData(i,:);
if Chest_Pain_Type <=3 && Induced_Angina <=0 && Age <=55 && Chest_Pain_Type <=1 && Gender <=0
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age <=55 && Chest_Pain_Type <=1 && Gender >0 && Resting_ECG <=1 && Chest_Pain_Type <=46
Patient0utput = 1;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age <=55 && Chest_Pain_Type <=1 && Gender >0 && Resting_ECG <=1 && Chest_Pain_Type >46
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age <=55 && Chest_Pain_Type <=1 && Gender >0 && Resting_ECG >1
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age <=55 && Chest_Pain_Type >1
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG <=0
Patient0utput = 1;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender <=0
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG <=0 && Chest_Pain_Type <=1
Patient0utput = 1;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG <=0 && Chest_Pain_Type >1 && Resting_BP <=128 && Max_HR <=142
Patient0utput = 1;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG <=0 && Chest_Pain_Type >1 && Resting_BP <=128 && Max_HR >142
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG <=0 && Chest_Pain_Type >1 && Resting_BP >128
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG >0 && Resting_ECG <=1
Patient0utput = 1;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG >0 && Resting_ECG >1 && Fluoroscopy_Vessels <=0 && Resting_ECG <=271
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG >0 && Resting_ECG >1 && Fluoroscopy_Vessels <=0 && Resting_ECG >271
Patient0utput = 1;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG >0 && Resting_ECG >1 && Fluoroscopy_Vessels >0
Patient0utput = 1;
elseif Chest_Pain_Type <=3 && Induced_Angina >0 && ST_Slope <=1
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina >0 && ST_Slope >1
Patient0utput = 1;
elseif Chest_Pain_Type >3 && Serum_Cholesterol <=0
Patient0utput = 1;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression <=0.8 && Gender <=0 && Induced_Angina <=0
Patient0utput = 0;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression <=0.8 && Gender <=0 && Induced_Angina >0 && Resting_ECG <=0
Patient0utput = 1;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression <=0.8 && Gender <=0 && Induced_Angina >0 && Resting_ECG >0
Patient0utput = 0;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression <=0.8 && Gender >0 && Fluoroscopy_Vessels <=0 && Heart_Condition <=3
Patient0utput = 0;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression <=0.8 && Gender >0 && Fluoroscopy_Vessels <=0 && Heart_Condition >3
Patient0utput = 1;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression <=0.8 && Gender >0 && Fluoroscopy_Vessels >0
Patient0utput = 1;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression >0.8 && Gender <=0 && Heart_Condition <=3 && Induced_Angina <=0
Patient0utput = 0;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression >0.8 && Gender <=0 && Heart_Condition <=3 && Induced_Angina >0
Patient0utput = 1;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression >0.8 && Gender <=0 && Heart_Condition >3
Patient0utput = 1;
else
Patient0utput = 1;
end
if Patient0utput <1
disp 'This patient does not have cardiovascular disease';
else
disp 'This patient has cardiovascular disease';
end
end
I was told to use thisrow in the decision tree but where do I put this in?
This is the error that comes up when i go to run the code
Undefined function or variable 'readMatrix'.
Error in Testing (line 1)
myData = readMatrix('BME501_Coursework_Testdata.csv');
1 comentario
dpb
el 7 de Dic. de 2020
Editada: dpb
el 7 de Dic. de 2020
Please return to the original Q? that Walter gave guidance for --
What Walter suggested was to turn your code into a function named thisthrow and pass the input data to it by row.
The note by Harry is definitely true; looks like you may intend some ORs in there instead of all ANDs. On going back to original, I see the same logic therein; just the name change appears to be all.
I noticed it but hadn't tried to make sense out of it as I was trying to see about building the logic tree...it could be possible to first select for <=3 and then subset that to <=1, but not with the logic as written as AND--those that are <=1 are also <=3 so both are true which leads, I suspect, to a result not what is intended with the compound comparison.
Also, you want '&' here, not '&&'
Respuesta aceptada
Harry Laing
el 7 de Dic. de 2020
I've never heard of the function readMatrix. I'd suggest using the function readtable if your CSV file has headings as well as data. (This answer here on importing CSV data may also help). You also need to refer to the column (or table heading) and row number in your if conditions.
In the example below, I assume your csv file has the same heading names you used in your if statemets:
myData = readtable('myfile.csv');
[numRows, numCols] = size(myData);
for i = 1:NumRows
if myData.Chest_Pain_Type(i) <=3 && myData.Induced_Angina(i) <=0 && myData.Age(i) <=55 && myData.Chest_Pain_Type(i) <=1 && GmyData.Gender(i) <=0
Patient0utput = 0;
elseif myData.Chest_Pain_Type(i) <=3 && myData.Induced_Angina(i) <=0 && myData.Age(i) <=55 && myData.Chest_Pain_Type(i) <=1 && myData.Gender(i) >0 && myData.Resting_ECG(i) <=1 && myData.Chest_Pain_Type(i) <=46
Patient0utput = 1;
% etc etc etc
end
Note: Although, I must say, you seem the have mutliple statements that are useless in some of your if conditions. For example, it seems pointless to me to have both
Chest_Pain_Type <= 3
and
Chest_Pain_Type <= 1
because if the value is less than or equal to 1, then the condition of being less than or equal to 3 is useless?
9 comentarios
Walter Roberson
el 9 de Dic. de 2020
Any time you have an "if" that is testing a vector you probably want to turn it into logical indexing instead.
Harry Laing
el 9 de Dic. de 2020
To quote your error:
Error in BME501_CourseworkPartA_Ronan_Rafferty_B00748725 (line 136)
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 &&
Serum_Cholesterol >0 && Gender >0 && Resting_ECG >0 && Resting_ECG
>1 && Fluoroscopy_Vessels <=0 && Serum_Cholesterol <=271
In the error you have, you appear to be referring to a variable "Chest_Pain_Type" (and so on). Is this variable a single value (scalar) or does it contain multiple values (a vector or matrix)? Are you intending it to be a vector of values or just a single value? Same question for every variable name. Run the script with breakpoints to debug the code and confirm this for yourself.
If it is a vector of all the data, then using && wont work because && requires scalar values either side. I believe you are intending for each variable to be a single value, in which case using && should still work. Though as walter said, logical indexing would make sense here.
Más respuestas (1)
dpb
el 7 de Dic. de 2020
MATLAB is case sensitive--the function is readmatrix, not readMatrix
0 comentarios
Ver también
Categorías
Más información sobre Functions en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!