This code is suppose to loop through an excel file and tell if each vaule in the second column is either impaired or not. with 1 being impaired and 0 being fine. However it is only looking at the first value instead of looping through all of them.
2 views (last 30 days)
Show older comments
clear,clc
num = xlsread('Hwk1_LDL.xlsx');
m = num
t = m(:,2)
for i = 2:50
if t >= 130
a = 1
else
a = 0
end
end
These are the instructions for the program. Can someone tell me what is missing so that it will loop through the whole column?
Make a program that:
- o Automatically imports the excel file from the from the MatLab folder to a matrix called ‘LDL’
- o Loops through each of the samples in LDL and checks to see if it is classified as impaired or not.
- o Using column 3 in LDL (a new column) assign a value of 0 for acceptable LDL and 1 for high LDL.
2 Comments
Answers (2)
Geoff Hayes
on 4 Sep 2017
Courtney - if you are iterating over each element of the t (which is the second column of your input data) then you need to access each element using indexing (see array indexing for more details). For example,
for k=1:length(t)
if t(k) >= 130
% do something
else
% do something else
end
end
We iterate over each element of t and access the element using k as t(k).
0 Comments
Image Analyst
on 4 Sep 2017
It's a very poorly worded problem - lots of ambiguous/inconsistent wording. Anyway, instead of
num = xlsread('Hwk1_LDL.xlsx');
you need
LDL = xlsread('Hwk1_LDL.xlsx');
because that's what the instructions directed you to do.
Then you need to loop over all elements in column 2 to see if they're "impaired" but I don't know what that means but I guess it means they're more than 130. You can do it vectorized but because is said to do it via a loop, you must use a for loop. And assuming "not impaired" is "acceptable" and "impaired" is high, then you can assign the third column of LDL.
col2 = LDL(:, 2); % Extract column 2 for convenience
for row = 1 : length(col2)
if col2(row) > 130
% This element is impaired.
LDL(row, 3) = 1;
else
% Acceptable
LDL(row, 3) = 0;
end
end
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!