loop thar places zero in the column of matrix if row is bigger than 2 in matlab

You can loop through each element of the matrix and check if the row index is greater than 2. If it is, then set the value of that element to zero. Here's an example code to do that:

main.m
% Create a sample matrix
matrix = [1 2 3; 4 5 6; 7 8 9; 10 11 12];

% Get the size of the matrix
[rows, cols] = size(matrix);

% Loop through each element of the matrix
for i = 1:rows
    for j = 1:cols
        % Check if the row index is greater than 2
        if i > 2
            % Set the value of the element to zero
            matrix(i, j) = 0;
        end
    end
end

% Print the modified matrix
disp(matrix);
419 chars
20 lines

This code will replace the elements in rows greater than 2 with zeros. The output of this code would be:

main.m
 1     2     3
 4     5     6
 0     0     0
 0     0     0
60 chars
5 lines

Hope this helps!

related categories

gistlibby LogSnag