This can be done many ways. You can use nested for loops, like you said, or conditional assignments as Rodrigo suggests.
First, here's your data:
%// your matrix
M = rand(500,5)*100;
You'll want to know the number of rows and columns for the loop...
%// get the size for the loops
[num_rows,num_columns] = size(M);
To simply loop through one column, the following should work:
%// loop through one column (column #2), save one result
col = 2;
for row = 1:num_rows
if M(row,col) <= 80
age = 1;
else
age = 2;
end
end
But, you said you wanted to descretize the data, so you'll likely want to change save all the results. The above example will leave you with a variable called 'age' which will only store that last value from the loop.
The following should allow you to save all the results from the individual column:
%// loop through one column (column #2), save all results
col = 2;
%// initialize age array
age = zeros(500,1);
%// do the loop
for row = 1:num_rows
if M(row,col) <= 80
age(row) = 1;
else
age(row) = 2;
end
end
To loop through all the columns requires another for loop:
%// loop through all columns, save all results
%// initialize age array
age = zeros(500,5);
%// loop through each column
for col = 1:num_columns
%// loop through each row
for row = 1:num_rows
if M(row,col) <= 80
age(row,col) = 1;
else
age(row,col) = 2;
end
end
end
Finally, now that you've seen that, the preferred way for many is to take advantage of MATLAB's conditional assignment tricks. The following will produce the same result as the last code snippet:
%// now without loops
age = zeros(500,5);
age(M <= 80) = 1;
age(M > 80) = 2;
(note that I've used % and // in my comments... you can ignore the // since I only added it so Stack Exchange would recognize my comments)
To answer your follow up, its not necessary to add the bit about young = 1
and old = 2
. It is preferred, however, since it allows you to remove the Magic Numbers.
EDIT to answer follow-up:
To save the results in the original array, you can do a few things:
- You can assign the new array to a column of the old array at the end
- You can assign the new values as you loop
- You can use the conditional replacement Rodrigo mentioned.
The first one is easy... use one of the last two procedures above, then do this:
M(:,col_to_replace) = age(:,col_to_replace_with);
Or you could add a new column all together:
M(:,6) = age(:,col_of_interest);
Alternatively, you can just change the loop so the original values are replaced with their discretized value:
%// loop through all columns, save all results in original locations
%// loop through each column
for col = 1:num_columns
%// loop through each row
for row = 1:num_rows
if M(row,col) <= 80
M(row,col) = 1;
else
M(row,col) = 2;
end
end
end
Finally, you can just use the conditional replacement method. The sample below will replace all the rows and columns of M with the discretized value:
M(M <= 80) = 1;
M(M > 80) = 2;
To answer your specific example, this will loop through column 1 and save the result in column 3:
%// loop through one column (column #1), save all results in another column (#3)
col = 1;
save_col = 3;
%// do the loop
for row = 1:num_rows
if M(row,col) <= 80
M(row,save_col) = 1;
else
M(row,save_col) = 2;
end
end