Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

First question: Enter a vector, named vector1 , which has at least 6 elements, u

ID: 3550175 • Letter: F

Question

First question: Enter a vector, named vector1, which has at least 6 elements, using the input statement. Create a new vector, named vector2, containing only the elements in the even numbered positions from vector1. For example, if vector1 is [0 1 2 3 4 5 6], vector2 should be [1 3 5].


second question: Write a MATLAB program (script) to add all the elements of a matrix. Do not use the MATLAB sum() function. Make the program general so that it will work with any size matrix.




These are two different questions that I am confused on. Please help, thanks!!!

I am pretty sure I am overthinking the solution.



Explanation / Answer

% First question: Enter a vector, named vector1, which has at least 6 elements, using the input statement.
% Create a new vector, named vector2, containing only the elements in the even numbered positions from vector1.
% For example, if vector1 is [0 1 2 3 4 5 6], vector2 should be [1 3 5].

vector1 = input('Input your vector:');
vector2 = vector1(2:2:end)


%second question: Write a MATLAB program (script) to add all the elements of a matrix. Do not use the MATLAB sum() function.
%Make the program general so that it will work with any size matrix.

% if that is case better write a function for sum that takes care of any type of input.

A = input('Enter your array');
[rowsM,colsM] = size(A);
sum = 0;
for i=1:rowsM
for j=1:colsM
sum = sum + A(i,j);
end
end
disp(" Sum of all elements is ");
disp(sum);