in a matrix set the column from the left to the column number from an unlimited range in matlab

To set a column from the left up to a certain column number in a matrix, you can use indexing in Matlab.

Assuming we have a matrix called A, we can set the first n columns by using:

main.m
A(:, 1:n) = [new_values];
26 chars
2 lines

where n is the column number up to which we want to set values and [new_values] is a matrix of the same size as the selected columns in A.

For example, if we want to set the first 5 columns of A, we can do:

main.m
A(:, 1:5) = [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15];
53 chars
2 lines

This will set the first 5 columns of A to the values [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15].

Note that the number of rows in [new_values] must match the number of rows in the selected columns of A, otherwise you will get an error.

gistlibby LogSnag