addition of two matrices without using functions in matlab

To perform the addition of two matrices without using functions in MATLAB, you can do the following steps:

Step 1: Define the two matrices as 2-dimensional arrays.

main.m
% Define the matrices A and B
A = [1 2 3; 4 5 6; 7 8 9];
B = [9 8 7; 6 5 4; 3 2 1];
84 chars
4 lines

Step 2: Create an empty matrix called C with the same size as matrices A and B.

main.m
% Create an empty matrix C with the same size as A and B
C = zeros(size(A));
77 chars
3 lines

Step 3: Use nested loops to iterate over each element of matrices A and B, and perform the addition element-wise.

main.m
% Add matrices A and B element-wise and store the result in C
for i = 1:size(A, 1)
    for j = 1:size(A, 2)
        C(i, j) = A(i, j) + B(i, j);
    end
end
157 chars
7 lines

Step 4: Display the resulting matrix C which is the addition of matrices A and B.

main.m
% Display the resulting matrix C
disp(C);
42 chars
3 lines

The output will be:

10 10 10
10 10 10
10 10 10
27 chars
4 lines

This is the element-wise addition of matrices A and B.

Hope this helps!

related categories

gistlibby LogSnag