How can I add an index value to an array value?

조회 수: 1(최근 30일)
dormant
dormant 2022년 6월 17일
댓글: dormant 2022년 6월 17일
I have an array of reals, like this.
0.1 0.1 0.1 0.1
0.2 0.2 0.2 0.2
0.1 0.2 0.3 0.4
How can I add the horizontal index value, so that the array ends up like this?
1.1 2.1 3.1 4.1
1.2 2.2 3.2 4.2
1.1 2.2 3.3 4.4
I could easily do it with a loop, but I'm convinced there is a one-line solution. I just can't see it.
PS the array is a lot larger than the example.

채택된 답변

Stephen23
Stephen23 2022년 6월 17일
편집: Stephen23 2022년 6월 17일
M = [0.1,0.1,0.1,0.1;0.2,0.2,0.2,0.2;0.1,0.2,0.3,0.4]
M = 3×4
0.1000 0.1000 0.1000 0.1000 0.2000 0.2000 0.2000 0.2000 0.1000 0.2000 0.3000 0.4000
M = M + (1:size(M,2))
M = 3×4
1.1000 2.1000 3.1000 4.1000 1.2000 2.2000 3.2000 4.2000 1.1000 2.2000 3.3000 4.4000
  댓글 수: 1
dormant
dormant 2022년 6월 17일
Many thanks. I knew it should be simple.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by