How do I make a column vector to add to my original matrix?
조회 수: 5 (최근 30일)
이전 댓글 표시
Im trying to solve this: You want to add 4 to each element in the first row of A, subtract 1 from each element in the second row of A, and keep the third row as-is. Create a column vector that you can add to A to perform this task. Call your column vector B.
I have the matrix: A = [1 3 5; -10 -8 -6; (sin(pi/2)) 5^3 (exp(-2))]
I then did:
A = [1 3 5; -10 -8 -6; (sin(pi/2)) 5^3 (exp(-2))]
B_one = A(1,:) + 4
B_two = A(2,:) - 1
B_three = [(sin(pi/2)) 5^3 (exp(-2))]
B = [B_one B_two B_three]'
I am trying to make the column vector but my column vector is never the right size and I keep getting the error: Variable B must be of size [3 1]. It is currently of size [9 1]. Check where the variable is assigned a value.
댓글 수: 0
채택된 답변
추가 답변 (1개)
Voss
2023년 2월 5일
A = [1 3 5; -10 -8 -6; (sin(pi/2)) 5^3 (exp(-2))];
B_one = A(1,:) + 4;
B_two = A(2,:) - 1;
B_three = [(sin(pi/2)) 5^3 (exp(-2))];
In constructing B from B_one, B_two, B_three, use vertical concatenation by separating the rows with semicolons, and avoid transposing the result:
B = [B_one; B_two; B_three]
You can construct a column vector the same way, using vertical concatenation, which can then be added to A. Example:
A = magic(3)
to_add = [2; 5; -9] % this column vector will be used to add 2 to the first row of A, 5 to the 2nd, -9 to the 3rd
B = A + to_add
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!