How do I do addition on only the first two columns of an array?

1 view (last 30 days)
I have an array (attached: motion) that is a 2 x 1 x 20 double. I want to subtract [20 20] from the first two columns, but preserve the third. So for example, for one row:
motion = [18 20 1]
marker = [20 20]
result of motion - marker = [-2 0 1]
row two would look like:
motion = [19 21 2]
marker = [20 20]
result of motion - marker = [-1 1 2]
I am sure this is simple but I am making it more difficult than it needs to be I think. Thanks!

Accepted Answer

the cyclist
the cyclist on 17 May 2018
I'm pretty confused by your question, and in particular how you are referring to your "columns" and "rows". I downloaded your MAT file. The array Trans has 2 rows (the 1st dimension), 1 column (the 2nd dimension), and "depth" of 20 (the third dimension).
Trans(:,:,1) is
[20;
20]
and Trans(:,:,20) is
[21;
19]
Are you saying that you want those to become
[0;
0]
and
[ 1;
-1]
respectively?
If so, that is very simple.
out = Trans - 20;
If that is not what you mean, please give some more detail about what you want for output.
  1 Comment
geog170
geog170 on 17 May 2018
Thank you for weeding through my request, I just started using matlab regularly. This is exactly what I needed:
out = Trans - 20;

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices 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!