arrays - Matlab - Nested loop for certain column -



arrays - Matlab - Nested loop for certain column -

i have dataset array m of size 500x5, there way can utilize nested for loop run through particular column of array? if how go doing that? want if statement in loop like:

if age <= 80 age = 1 else age = 2 end

what set for loop? improve initialise variables young =1; old = 2; , have age = young in if statement rather age = 1? trying discretise info either 1 or 2 1 beingness young , 2 beingness old.

this can done many ways. can utilize nested loops, said, or conditional assignments rodrigo suggests.

first, here's data:

%// matrix m = rand(500,5)*100;

you'll want know number of rows , columns loop...

%// size loops [num_rows,num_columns] = size(m);

to loop through 1 column, next should work:

%// loop through 1 column (column #2), save 1 result col = 2; row = 1:num_rows if m(row,col) <= 80 age = 1; else age = 2; end end

but, said wanted descretize data, you'll want alter save results. above illustration leave variable called 'age' store lastly value loop.

the next should allow save results individual column:

%// loop through 1 column (column #2), save results col = 2; %// initialize age array age = zeros(500,1); %// loop row = 1:num_rows if m(row,col) <= 80 age(row) = 1; else age(row) = 2; end end

to loop through columns requires loop:

%// loop through columns, save results %// initialize age array age = zeros(500,5); %// loop through each column col = 1:num_columns %// loop through each row row = 1:num_rows if m(row,col) <= 80 age(row,col) = 1; else age(row,col) = 2; end end end

finally, you've seen that, preferred way many take advantage of matlab's conditional assignment tricks. next produce same result lastly code snippet:

%// without loops age = zeros(500,5); age(m <= 80) = 1; age(m > 80) = 2;

(note i've used % , // in comments... can ignore // since added stack exchange recognize comments)

to reply follow up, not necessary add together bit young = 1 , old = 2. preferred, however, since allows remove magic numbers.

edit reply follow-up:

to save results in original array, can few things:

you can assign new array column of old array @ end you can assign new values loop you can utilize conditional replacement rodrigo mentioned.

the first 1 easy... utilize 1 of lastly 2 procedures above, this:

m(:,col_to_replace) = age(:,col_to_replace_with);

or add together new column together:

m(:,6) = age(:,col_of_interest);

alternatively, can alter loop original values replaced discretized value:

%// loop through columns, save results in original locations %// loop through each column col = 1:num_columns %// loop through each row row = 1:num_rows if m(row,col) <= 80 m(row,col) = 1; else m(row,col) = 2; end end end

finally, can utilize conditional replacement method. sample below replace rows , columns of m discretized value:

m(m <= 80) = 1; m(m > 80) = 2;

to reply specific example, loop through column 1 , save result in column 3:

%// loop through 1 column (column #1), save results in column (#3) col = 1; save_col = 3; %// loop row = 1:num_rows if m(row,col) <= 80 m(row,save_col) = 1; else m(row,save_col) = 2; end end

arrays matlab loops if-statement nested

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -