I need to know how to sum to the values ​​of a specific column (identified by an index that can vary) of a generic matrix, a given column vector. For instance, how i decrease by 1 all the values ​​of a column in a matrix (the rest remains the same)?

1 view (last 30 days)
A=magic(10)
v=[1;1;1;1;1;1;1;1;1;1]
x=6
How i sum/subtract by 1 (the vector v or any generic one) to the column of A number x

Accepted Answer

Stephen23
Stephen23 on 19 Sep 2018
Edited: Stephen23 on 19 Sep 2018
Your question is rather confusing, as you seem to pose two different questions. I address both of these below:
>> A = magic(10)
A =
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
85 87 19 21 3 60 62 69 71 28
86 93 25 2 9 61 68 75 52 34
17 24 76 83 90 42 49 26 33 65
23 5 82 89 91 48 30 32 39 66
79 6 13 95 97 29 31 38 45 72
10 12 94 96 78 35 37 44 46 53
11 18 100 77 84 36 43 50 27 59
>> col = 6;
>> sum(A(:,col)) % the sum of one column
ans = 505
>> A(:,col) = A(:,col)-1 % decrease all values in one column
A =
92 99 1 8 15 66 74 51 58 40
98 80 7 14 16 72 55 57 64 41
4 81 88 20 22 53 56 63 70 47
85 87 19 21 3 59 62 69 71 28
86 93 25 2 9 60 68 75 52 34
17 24 76 83 90 41 49 26 33 65
23 5 82 89 91 47 30 32 39 66
79 6 13 95 97 28 31 38 45 72
10 12 94 96 78 34 37 44 46 53
11 18 100 77 84 35 43 50 27 59

More Answers (0)

Community Treasure Hunt

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

Start Hunting!