how to call out single element in matrix

12 Ansichten (letzte 30 Tage)
Noriham B
Noriham B am 29 Jul. 2022
Kommentiert: Walter Roberson am 29 Jul. 2022
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 29 Jul. 2022
A(1:2:end) + A(2:2:end) + B
  2 Kommentare
Noriham B
Noriham B am 29 Jul. 2022
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 am 29 Jul. 2022
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])

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by