create a matrix with 3 rows and 4 columns starting at 1 and increasing by 1 in matlab

You can create a matrix with 3 rows and 4 columns in MATLAB and fill it with values starting at 1 and increasing by 1 using a for loop. Here's how you can do it:

main.m
% Initialize the matrix
matrix = zeros(3, 4);

% Fill the matrix with values
value = 1;
for row = 1:3
    for col = 1:4
        matrix(row, col) = value;
        value = value + 1;
    end
end

% Display the matrix
disp(matrix);
229 chars
15 lines

In the above code, we first initialize a 3x4 matrix matrix with all elements set to 0 using the zeros function. Then we use nested for loops to iterate over each element of the matrix. The variable value keeps track of the current value to be assigned to each element, which starts at 1. Inside the loop, we assign value to the current element and then increment value by 1. Finally, we display the matrix using the disp function.

Running this code will create a matrix with elements starting from 1 and increasing by 1:

main.m
     1     2     3     4
     5     6     7     8
     9    10    11    12
75 chars
4 lines

Hope this helps!

related categories

gistlibby LogSnag