how to call out single element in matrix

10 views (last 30 days)
Dear Prof/Dr./Sir./Maam
I have matrix A=[1 2 3 4 5 6 ] and B=[ 7 8 9]. I want to calculate s1=1+2+7 s2=3+4+8 s3=5+6+9
Is it possible to calculate the elements from different matrix like that?
Thank you in advance

Accepted Answer

Walter Roberson
Walter Roberson on 29 Jul 2022
A(1:2:end) + A(2:2:end) + B
Walter Roberson
Walter Roberson on 29 Jul 2022
MATLAB has an operator that it calls "colon" that is available in two forms.
means the sequence of values starting from A, and add one each time, ending with the last number that is not greater than B. So for example 2.1:4 would be 2.1 then 3.1 and then when it internally looked at 4.1 it would be greater than the 4 endpoint so that sequence would stop with just the two values.
is closely related. If B is positive then you start from A, add B each step, and end before exceeding C. If B is negative such as 5:-1:3 then you add B each step (so you are subtracting) but you end when the result would be less than C, so 5:-1:3 would be 5 4 3
1:2:end thus means starting at 1 and add 2 each time, so 1 3 5 and so on. The "end" in A(1:2:end) stands in for the maximum index for that array. With A being 6 long A(1:2:end) would be A(1:2:6) which would be A([1 3 5]) . A(2:2:end) with length 6 would be A(2:2:6) or A([2 4 6])

Sign in to comment.

More Answers (0)


Find more on Creating and Concatenating 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!