How do cumulatively add a matrix column based on a vector

1 次查看(过去 30 天)
Hello all, I have the following matrix,
t =
1.0000 7.0000 0 0
2.0000 9.0000 0.6000 3.0000
3.0000 11.0000 -0.5000 -0.5000
4.0000 13.0000 0.6000 2.4000
5.0000 14.0000 -0.7000 -2.5000
6.0000 18.0000 0.7000 2.4000
7.0000 19.0000 -0.2000 1.0000
8.0000 20.0000 0 1.0000
9.0000 22.0000 0.7000 2.1000
10.0000 23.0000 -0.1000 1.4000
11.0000 24.0000 0 1.4000
12.0000 25.0000 0.2000 0.4000
13.0000 26.0000 0 0.4000
14.0000 28.0000 0.5000 0.5000
15.0000 29.0000 -0.2000 -0.9000
16.0000 30.0000 0.2000 0.5000
17.0000 31.0000 -0.1000 -0.2000
18.0000 32.0000 0.3000 0
19.0000 33.0000 -0.3000 -2.1000
20.0000 34.0000 0.2000 -0.7000
21.0000 35.0000 -0.1000 -1.4000
22.0000 39.0000 0 -1.4000
23.0000 42.0000 -0.3000 -3.5000
24.0000 44.0000 0.3000 -1.4000
25.0000 45.0000 -0.2000 -2.8000
& a vector m, corresponding to column 1 of t, as
m = [1 2 4 9 12 14 18];
So, I need to cumulatively add the values in column 3 of matrix t from
0 (1+1 : 2-1), that is m(1)+1 : m(2)-1
3 (2+1 : 4-1)
5, 6, 7, 8 (4+1 : 9-1)
10, 11 (9+1 : 12-1)
13 (12+1 : 14-1)
15, 16, 17 (14+1 : 18-1)
19 to 25 (18+1 : end)
& put these in column 4. Right now it has incorrect values. The starting points should all be 0. column 3 (m(n)) = 0, meaning m(1) = 0; m(2) = 0, m(4) = 0, etc.
My method with loops produces a lot of overlap & doesn't quite work. Hope this is not too confusing.
Any suggestions? Thanks Damo Nair
  3 个评论
Damo Nair
Damo Nair 2018-1-7
Ok, sorry about that Stephen ... here is a better explanation (I hope) of the output I need ...
This is what col 4 would look like ...
0 (because m(1) = 1) 0 (because m(2) = 2) -0.5 (got it from col 3, result of cumsum) 0 (because m(3) = 4) -0.7 0 -0.2 -0.2 0 (m(4) = 9) -0.1 0 0 (m(5) = 12) 0 0 (m(6) = 14) -0.2 0 -0.1 0 (m(end) = 18) -0.3 -0.1 -0.2 -0.2 -0.5 -0.2 -0.4
So, what is happening is that for rows of m there is a reset to 0 & in between the indices of m there is a cumsum. I hope this is a bit clearer.
Thanks again. Damo Nair
Matt J
Matt J 2018-1-8
@Damo, I recommend that you edit your post with t displayed with the correct data (manually entered by you).

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2018-1-8
Try this:
t = [...
1.0000 7.0000 0 0
2.0000 9.0000 0.6000 3.0000
3.0000 11.0000 -0.5000 -0.5000
4.0000 13.0000 0.6000 2.4000
5.0000 14.0000 -0.7000 -2.5000
6.0000 18.0000 0.7000 2.4000
7.0000 19.0000 -0.2000 1.0000
8.0000 20.0000 0 1.0000
9.0000 22.0000 0.7000 2.1000
10.0000 23.0000 -0.1000 1.4000
11.0000 24.0000 0 1.4000
12.0000 25.0000 0.2000 0.4000
13.0000 26.0000 0 0.4000
14.0000 28.0000 0.5000 0.5000
15.0000 29.0000 -0.2000 -0.9000
16.0000 30.0000 0.2000 0.5000
17.0000 31.0000 -0.1000 -0.2000
18.0000 32.0000 0.3000 0
19.0000 33.0000 -0.3000 -2.1000
20.0000 34.0000 0.2000 -0.7000
21.0000 35.0000 -0.1000 -1.4000
22.0000 39.0000 0 -1.4000
23.0000 42.0000 -0.3000 -3.5000
24.0000 44.0000 0.3000 -1.4000
25.0000 45.0000 -0.2000 -2.8000];
% Extract column 1 of t.
column1 = t(:, 1);
% & a vector m, corresponding to column 1 of t, defined as
m = [1 2 4 9 12 14 18];
for k = 1 : length(m) - 1
index1 = m(k) + 1;
index2 = m(k + 1) - 1;
theSum(k) = sum(column1(index1:index2));
fprintf('Summing column1 from index %d to %d and getting %.1f.\n', index1, index2, theSum(k));
end
You get:
Summing column1 from index 2 to 1 and getting 0.0.
Summing column1 from index 3 to 3 and getting 3.0.
Summing column1 from index 5 to 8 and getting 26.0.
Summing column1 from index 10 to 11 and getting 21.0.
Summing column1 from index 13 to 13 and getting 13.0.
Summing column1 from index 15 to 17 and getting 48.0.
  2 个评论
Image Analyst
Image Analyst 2018-1-8
So, (with your modifications), put this before the loop:
column3 = t(:, 3);
then, in the loop
theSum(k) = sum(column3(index1:index2)); % Sum column3
% Set column 4 of t to zero for this m value.
t(m(k), 4) = 0;
Damo Nair
Damo Nair 2018-1-8
Yes, thanks Image Analyst, I changed that to col 3 & sum to cumsum, added some end effects & it works now!
Have a good day. Damo.

请先登录,再进行评论。

更多回答(1 个)

Damo Nair
Damo Nair 2018-1-8
编辑:Damo Nair 2018-1-8
Thanks, Stephen, Matt & Image Analyst, very sorry for the clumsiness! Here at last is the correct t matrix ...
t =
1.0000 7.0000 0 0
2.0000 9.0000 0.6000 0
3.0000 11.0000 -0.5000 -0.5000
4.0000 13.0000 0.6000 0
5.0000 14.0000 -0.7000 -0.7000
6.0000 18.0000 0.7000 0
7.0000 19.0000 -0.2000 -0.2000
8.0000 20.0000 0 -0.2000
9.0000 22.0000 0.7000 0
10.0000 23.0000 -0.1000 -0.1000
11.0000 24.0000 0 -0.1
12.0000 25.0000 0.2000 0
13.0000 26.0000 0 0
14.0000 28.0000 0.5000 0
15.0000 29.0000 -0.2000 -0.2000
16.0000 30.0000 0.2000 0
17.0000 31.0000 -0.1000 -0.1000
18.0000 32.0000 0.3000 0
19.0000 33.0000 -0.3000 -0.3000
20.0000 34.0000 0.2000 -0.1000
21.0000 35.0000 -0.1000 -0.2000
22.0000 39.0000 0 -0.2000
23.0000 42.0000 -0.3000 -0.5000
24.0000 44.0000 0.3000 -0.2000
m = [1 2 4 9 12 14 18];
In col 4 I am resetting to 0 at the m values. In between the m indices I am trying to do a cumulative sum of column 3. I hope its a bit clearer this time.
Thanks, appreciate the help. Damo Nair

类别

Help CenterFile Exchange 中查找有关 Geometric Transformation and Image Registration 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by