Given a matrix "A", how to create a row vector of 1s that has the same number of elements as "A" has rows?

258 ビュー (過去 30 日間)
Given a matrix "A", do the following things:
  1. Create a row vector of 1's that has the same number of elements as "A" has rows.
  2. Create a column vector of 1's that has the same number of elements as "A" has columns.
  3. 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];

採用された回答

Manali Gupta
Manali Gupta 2024 年 10 月 21 日
編集済み: MathWorks Support Team 2024 年 10 月 21 日
Given a matrix "A", you can find the number of rows of A by using "size(A,1)". You can find the number of columns of "A" by using "size(A,2)". You can then use "ones" to create a row or column vector of 1's. Finally, use the "mtimes" or * operator to perform matrix multiplication. 
A = [1:5; 6:10; 11:15; 16:20]; row_vector = ones(1,size(A,1)); column_vector = ones(size(A,2),1); result = row_vector*A*column_vector;
For more information about learning the basics of MATLAB®, see MATLAB Onramp
  4 件のコメント
Aadesh Navade
Aadesh Navade 2020 年 5 月 29 日
can you explain the syntax row and column
Mariam Aldeepa
Mariam Aldeepa 2021 年 1 月 2 日
Why when you calculate the result multiplied the matrix "A" with another two matrices ?

サインインしてコメントする。

その他の回答 (11 件)

Alex Mcaulley
Alex Mcaulley 2019 年 6 月 4 日
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 件のコメント
Carlo Romagnolo
Carlo Romagnolo 2021 年 4 月 19 日
Hi! I know this might be a long a shot, as this thread has been inactive for quite some time, but I was wondering if you could help me with a general question on this.
Here is my code to solve this problem:
A = [1:5; 6:10; 11:15; 16:20];
format compact
row = [1,1,1,1]
column = [1;1;1;1;1]
result = row * A * column
I was wondering if there is a better/more elegant way to answer the question which does not involve hardcoding the row and column vectors but instead uses functions.

サインインしてコメントする。


Khom Raj Thapa Magar
Khom Raj Thapa Magar 2020 年 8 月 28 日
編集済み: DGM 2023 年 2 月 21 日
% 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 件のコメント
DGM
DGM 2023 年 2 月 21 日
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.

サインインしてコメントする。


Sudhanshu Rasal
Sudhanshu Rasal 2020 年 5 月 5 日
編集済み: DGM 2023 年 2 月 21 日
Simple way to do this question is
X=[1 1 1 1]
Y=[1;1;1;1;1]
result=X*A*Y
????????
  3 件のコメント
el mehdi latrach
el mehdi latrach 2020 年 6 月 14 日
no, it does not work ,
it shows error
DGM
DGM 2023 年 2 月 21 日
編集済み: DGM 2023 年 2 月 21 日
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.

サインインしてコメントする。


Tushar Parmar
Tushar Parmar 2020 年 5 月 11 日
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 件のコメント
DGM
DGM 2023 年 2 月 21 日
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.

サインインしてコメントする。


Arakala Gautham
Arakala Gautham 2020 年 4 月 4 日
R_vector = ones(1,size(A,1));
C_vector=ones(size(A,2),1);
result = R_vector*A*C_vector;
  5 件のコメント
DGM
DGM 2023 年 2 月 21 日
Note that the accepted answer was copy-pasted verbatim from this answer.
Abhishek
Abhishek 2023 年 7 月 2 日
A = [1:5; 6:10; 11:15; 16:20];
R = (A(:,1))';
R(:)=1;
C = (A(1,:))';
C(:)=1;
result = R*A*C
%@DGM bro is this code ookay?

サインインしてコメントする。


Rishabh Nirala
Rishabh Nirala 2020 年 5 月 20 日
編集済み: DGM 2023 年 2 月 21 日
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 件のコメント
DGM
DGM 2023 年 2 月 21 日
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.

サインインしてコメントする。


Sneham Shrikant Vaidya
Sneham Shrikant Vaidya 2020 年 5 月 27 日
編集済み: DGM 2023 年 2 月 21 日
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 件のコメント
DGM
DGM 2023 年 2 月 21 日
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.

サインインしてコメントする。


VISHWA D
VISHWA D 2020 年 6 月 22 日
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 件のコメント
lijuan wang
lijuan wang 2020 年 8 月 27 日
I thought the assignment has a wrong requirement, right?
we should create a vector which has the some rows as A. So it should be row_vector=[1 1 1 1]'?
DGM
DGM 2023 年 2 月 21 日
編集済み: DGM 2023 年 2 月 21 日
@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.

サインインしてコメントする。


Chintan
Chintan 2023 年 2 月 21 日
row_vector=ones(size(A(:,1)))'
coloumn_vector=ones(size(A(1,:)))'
result=row_vector*A*coloumn_vector
  1 件のコメント
DGM
DGM 2023 年 2 月 21 日
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.

サインインしてコメントする。


Waseem Zafar
Waseem Zafar 2023 年 9 月 17 日
編集済み: Waseem Zafar 2023 年 9 月 17 日
A = [1:5; 6:10, 11:15; 16:20]
R = [1 1 1 1]
C = [1;1;1;1;1]
Z = A*C
result = R*Z
  1 件のコメント
DGM
DGM 2023 年 9 月 17 日
This example is identical to at least two other bad examples in this thread, differing only in the variable names.
You are told to use the size of A to construct two vectors. This example, and at least four others define the vectors literally instead of programmatically creating them as told. Let me be clear. You literally wrote two of the intermediate results into the script instead of making your script calculate them. If I were grading this, I would accept it no more than if you had just written
result = 210;
... because it neglects the entire point of the exercise to presuppose the input and bake the results into the script as literals.
An answer can earn its place if it demonstrates something either good or bad, but there is no value in duplicate, unformatted, unexplained answers with obvious, openly-discussed flaws. It doesn't add anything to the discussion and only serves to confuse readers who are looking for help.
Is there always room left in a thread for another answer that's not a near-duplicate? No. Sometimes it's not. Is this question completely exhausted? Not sure, but with extremely simple direct questions, it doesn't take long. If you want to answer questions, pick a thread where there is still room for a new answer.

サインインしてコメントする。


asmaa
asmaa 2024 年 2 月 2 日
Task
Create a row vector named d that contains sqrt(10) as the first element and pi^2 (
π2
) as the second elemen

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by