How do I make a column vector to add to my original matrix?
6 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
More Answers (1)
Voss
on 5 Feb 2023
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 Comments
See Also
Categories
Find more on Multidimensional Arrays 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!