MATLAB Answers

loop through matlab vector

74 views (last 30 days)
M4L
M4L on 1 Dec 2020
Commented: Rik on 3 Dec 2020
This question was flagged by Walter Roberson
Hey Guys. I am looking help with some code I wish to write. I have used code to write data from an Excel file manually by typing in each variable when prompted.

  3 Comments

Rik
Rik on 3 Dec 2020
Question originally posted by malachy magee retrieved from Google cache:
looping through rows one at a time putting each row through a set of if statements to create a table of outcomes
Hey Guys. I am looking help with some code I wish to write. I have used code to write a decision tree from weka and take data from an Excel file manually by typing in each variable when prompted.
I have done this code fine but I want to make this efficient and instead take the data from the Excel file, then row by row put it through my set of if and elseif statements of the tree. The aim is to determine the outcome of the patient based on numerous variables.
column 1 is age
column 2 is gender
column 3...etc for 13 columns
There are 90 patients so 90 rows.
Here is the start
file = uigetfile ('.csv');
table = readtable(file);
t = cell2mat(table2cell(table));
Then I assume I create variables from that matrix :
age = t(:,1);
gender = t(:,2);
chest_pain_type = t(:,3);
Here is where I get stuck. Also there are two possible outcomes T or F
patient_outcome = '';
for this_row = t.' %what do i do here i tried
if (chest_pain_type>3) & (serum_cholestoral>0) & (st_depression>0.8) & (gender>0)
patient_outcome = 'T';
elseif (chest_pain_type>3) & (serum_cholestoral>0) & (st_depression>0.8) & (gender<=0) & (heart_condition>3)
patient_outcome = 'T';
elseif (chest_pain_type>3) & (serum_cholestoral>0) & (st_depression>0.8) & (gender<=0) & (heart_condition<=3) & (exercise_induced_angina>0)
patient_outcome = 'F';
end
end
M4L
M4L on 3 Dec 2020
thanks I dunno why this removed parts of my question and title.
the answer worked :)
Rik
Rik on 3 Dec 2020
You can edit your question to put back the original title and content (and add the csv you attached to the comment you deleted).
Deleting things and changing the username is not generally an indication of good intentions, so go ahead and prove my cynicism wrong.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 1 Dec 2020
Attach a CSV file so we can get started helping you. In the meantime, you probably want to do something like
numRows = height(t); % or size(t, 1)
patient_outcome = false(numRows, 1);
for row = 1 : numRows
if (chest_pain_type(row)>3) &&&& (serum_cholestoral(row)>0) && (st_depression(row)>0.8) && (gender(row)>0)
patient_outcome(row) = true;
elseif (chest_pain_type(row)>3) && (serum_cholestoral(row)>0) && (st_depression(row)>0.8) && (gender(row)<=0) && (heart_condition(row)>3)
patient_outcome(row) = true;
elseif (chest_pain_type(row)>3) && (serum_cholestora(row)l>0) && (st_depression(row)>0.8) && (gender(row)<=0) && (heart_condition(row)<=3) && (exercise_induced_angina(row)>0)
patient_outcome(row) = false;
end
end

  1 Comment

M4L
M4L on 3 Dec 2020
Thanks again perfect

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!