Given a Matrix A, create a row vector of 1s that has same number of elements as A has rows

1.025 views (last 30 days)
Given a Matrix A, Create a row vector of 1's that has same number of elements as A has rows. Create a column vector of 1's that has the same number of elements as A has columns. Using matrix multiplication, assign the product of the row vector, the matrix A, and the column vector (in this order) to the variable result. A = [1:5; 6:10; 11:15; 16:20];

Accepted Answer

Manali Gupta
Manali Gupta on 20 May 2021
Edited: MathWorks Support Team on 20 May 2021
R_vector = ones(1,size(A,1)); C_vector=ones(size(A,2),1); result = R_vector*A*C_vector;
  4 Comments

Sign in to comment.

More Answers (9)

Alex Mcaulley
Alex Mcaulley on 4 Jun 2019
I think the goal of this forum is not to do anyone's homework, but to solve generic questions about Matlab that can help more users. This exercise is very simple, anyone who has ever used Matlab could do it, so, I think the best thing we can do is help @Gaurav to know how to learn the basics of Matlab.
  2 Comments

Sign in to comment.


Khom Raj Thapa Magar
Khom Raj Thapa Magar on 28 Aug 2020
Edited: DGM on 21 Feb 2023 at 7:26
% Given
A = [1:5; 6:10; 11:15; 16:20];
row_vec = ones(1,size(A,1))
row_vec = 1×4
1 1 1 1
column_vec = ones(length(A),1)
column_vec = 5×1
1 1 1 1 1
result = row_vec * A * column_vec
result = 210
  1 Comment
DGM
DGM on 21 Feb 2023 at 7:25
The length() function does not return the number of columns in an array. It returns the size of the longest dimension. If A has more rows than columns, this code will fail. Use size() to get the size of a specific dimension.

Sign in to comment.


Sudhanshu Rasal
Sudhanshu Rasal on 5 May 2020
Edited: DGM on 21 Feb 2023 at 7:58
Simple way to do this question is
X=[1 1 1 1]
Y=[1;1;1;1;1]
result=X*A*Y
????????
  3 Comments
DGM
DGM on 21 Feb 2023 at 7:34
Edited: DGM on 21 Feb 2023 at 7:37
The point of writing a program is to have the computer do the work. Literally writing out the vectors manually makes as much sense as calculating the inner product on paper and writing
result = 210; % this is my entire program
Both examples will fail for obvious reasons if the size of A changes.

Sign in to comment.


Tushar Parmar
Tushar Parmar on 11 May 2020
A = [1:5; 6:10; 11:15; 16:20]
B(1:4)=1;
f=B*A
C(1:5)=1;
C=C'
result=f*C
  6 Comments
DGM
DGM on 21 Feb 2023 at 7:38
The point of writing a program is to have the computer do the work. Use size() to find the size of A; use ones() to generate the vectors programmatically.

Sign in to comment.


Arakala Gautham
Arakala Gautham on 4 Apr 2020
R_vector = ones(1,size(A,1));
C_vector=ones(size(A,2),1);
result = R_vector*A*C_vector;
  4 Comments

Sign in to comment.


Rishabh Nirala
Rishabh Nirala on 20 May 2020
Edited: DGM on 21 Feb 2023 at 7:58
A = [1:5; 6:10; 11:15; 16:20];
C = [1;1;1;1;1]
R = [1 1 1 1 ]
P = R * A
result = P*C
ANSWER = 210
  1 Comment
DGM
DGM on 21 Feb 2023 at 7:39
Again, the point of writing a program is to have the computer do the work. Use size() to find the size of A; use ones() to generate the vectors programmatically.

Sign in to comment.


Sneham Shrikant Vaidya
Sneham Shrikant Vaidya on 27 May 2020
Edited: DGM on 21 Feb 2023 at 7:42
A = [1:5; 6:10; 11:15; 16:20];
A
x = [1 1 1 1 ]
y = [1;1;1;1;1]
z = A*y
result =x*z
you can also perform this way as we know z =(lxm)*(mxn) so we first multiply A*y as their inner dimension ara same
then we obtain result matrix z that has inner dimension equal to x so now we can multiply x*z to get final ans
  1 Comment
DGM
DGM on 21 Feb 2023 at 7:46
Use size() to find the size of A; use ones() to generate the vectors programmatically. Otherwise, this fails if the size of A changes.
Adding explanations to your answers is good though.

Sign in to comment.


VISHWA D
VISHWA D on 22 Jun 2020
A = [1:5; 6:10; 11:15; 16:20];
row_vector=[1 1 1 1 1]
col_vector=[1; 1; 1; 1]
result=(row_vector*(A'))*(col_vector)
  2 Comments
DGM
DGM on 21 Feb 2023 at 7:51
Edited: DGM on 21 Feb 2023 at 7:52
@lijuan wang is correct. This answer creates the vectors incorrectly. Not only are they created manually in a way that would break if the size of A changes, they do not correspond to the specified dimensions of A. The result is correct, but the vectors are wrong, and the method is poor practice.

Sign in to comment.


Chintan
Chintan on 21 Feb 2023
row_vector=ones(size(A(:,1)))'
coloumn_vector=ones(size(A(1,:)))'
result=row_vector*A*coloumn_vector
  1 Comment
DGM
DGM on 21 Feb 2023 at 8:06
I suppose that's one way, but size() supports dimension specification, which would avoid the need to address vectors of A or to transpose anything.
Also note that the ctranspose, ' operator is the complex conjugate transpose. If you just want to reorient a vector or matrix, use transpose, .' instead.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by