how to call out single element in matrix

11 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2022-7-29
A(1:2:end) + A(2:2:end) + B
  2 个评论
Noriham B
Noriham B 2022-7-29
thank you very much for ur help Prof./Dr./Sir...but, can u explain a little bit what is the meaning of 1:2 and the 2: 2 in the bracket here sir A(1:2:end) + A(2:2:end)
Walter Roberson
Walter Roberson 2022-7-29
MATLAB has an operator that it calls "colon" that is available in two forms.
A:B
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.
A:B:C
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])

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by