Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

How do I extract columns from a matrix using a loop?

Asked by Josh Bridges on 27 Nov 2012

Greetings,

I have a 15x2 matrix that looks like this:

1500 0

220 80

220 75

220 70

220 65

275 70

275 75

275 80

285 80

285 75

300 80

300 70

300 60

220 30

300 40

From this matrix, I have to separate the two columns into different variable in column vector form, but I don't need the first row(this is another task I was able to do) and I MUST use a loop per the instructions.

So far, I have:

for l = 1:d

    if datain(l)>100 
        if datain(l)<=300
        PH1(m) = datain(l);
        PH = PH1';
        m = m + 1;
        end
    end

end

m = m - 1;

for k = datain

    LA = k(2:15,1);

end

Technically, this works, somewhat, but I don't think it's what the professor is looking for. Any help would be greatly appreciated.

-Josh

1 Comment

Jan Simon on 28 Nov 2012

Please learn how to format code in the forum. Currently the code looks ugly.

Josh Bridges

Products

No products are associated with this question.

2 Answers

Answer by bym on 27 Nov 2012
Edited by bym on 27 Nov 2012
for k = 2:length(matrix)
var1(k-1) = matrix(k,1);
var2(k-1) = matrix(k,2);
end   % 

0 Comments

bym
Answer by Jan Simon on 28 Nov 2012

This runs, but does not do what you expect:

for k = datain
   LA = k(2:15,1);
end

For each element of datain the variable LA is overwritten by the vektor k(2:15,1). Therefore the FOR loop can be omitted.

What is d in your code?

Why do you assume, that the professor wants something else?

0 Comments

Jan Simon

Contact us