# How do I create a row vector P, that contains the sum of the values in column 2 and the sum of the values in column 5 of matrix B.

2 views (last 30 days)
Kyle Weaver on 19 Feb 2024
Commented: Kyle Weaver on 19 Feb 2024
Given: B=rand(randi([10 20]),randi([5 30]))
Find: Create a row vector P, that contains the sum of the values in column 2 and the sum of the values in column 5 of matrix B.
Issue: I am unfamiliar with the wording, or how to contain the two sums in one variable using code. Variable P must be of size [1 2].
My Solution: I got as far as to state >>P=sum(B(:,2); but I do not know how to get the required parameters.
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Accepted Answer

VBBV on 19 Feb 2024
B = rand(randi([10,20]),randi([5,30]))
B = 14x8
0.3610 0.2474 0.3160 0.1435 0.1535 0.3404 0.2446 0.2655 0.4167 0.9502 0.6448 0.7184 0.4979 0.9226 0.3517 0.7340 0.3727 0.5187 0.7976 0.0949 0.1334 0.4733 0.0156 0.9237 0.1379 0.4606 0.3640 0.2959 0.1628 0.0785 0.9126 0.5805 0.3026 0.0816 0.7866 0.7748 0.3589 0.4130 0.5204 0.7442 0.1768 0.3753 0.5468 0.7770 0.7853 0.1499 0.1178 0.6284 0.3736 0.8786 0.3791 0.8693 0.1866 0.7627 0.0871 0.4819 0.6952 0.9907 0.6566 0.2390 0.9916 0.5976 0.4159 0.1089 0.2989 0.7671 0.0724 0.0051 0.5487 0.5736 0.0606 0.3279 0.5183 0.3728 0.7747 0.3285 0.4035 0.6180 0.1434 0.6018
P = sum(B(:,[2,5]))
P = 1x2
8.2966 5.1808
##### 1 CommentShow -1 older commentsHide -1 older comments
Kyle Weaver on 19 Feb 2024
Eureka! I can't believe the answer is this simple. I forgot how to create a subset for the columns. Thank you!

Sign in to comment.

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!