how to call out single element in matrix

조회 수: 11 (최근 30일)
Noriham B
Noriham B 2022년 7월 29일
댓글: Walter Roberson 2022년 7월 29일
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