0
votes

I am just a beginner using MatLab. I wanted to add 2 matrices in which user inputs dimension of matrix and then inputs values.

Values are inserted element by element. I want user to input values row-wise, i.e., for a 2x3 matrix, user should input 2 lines, each line with 3 space separated integer values.

m = input('Enter no. of rows ');
n = input('Enter no. of columns ');
A = zeros(m, n);
B = zeros(m, n);
C = zeros(m, n);

disp('Enter elements in matrix A ');
for i=1 : m
    for j=1 : n
        A(i,j) = input('\');
    end
end
disp('Enter elements in matrix B ');
for i = 1 : m
    for j = 1 : n
        B(i, j) = input('\');
        C(i, j) = A(i, j) + B(i, j);
   end
end
clc;
disp('Matrix A is');
A
disp('Matrix B is');
B
disp('Matrix A + B is');
C

How shall i do that?

1

1 Answers

0
votes

You could do it using:

for j=1 : n
    A(:,j) = input('\');
end

Then the user has to enter a row like [1,2,3,4]

My recommendation is to ask for the full matrix at once. This way the user can enter a variable name which contains the intended function.