How do I add an integer to every or any nth row

조회 수: 49 (최근 30일)
hbcukid
hbcukid 2020년 11월 19일
댓글: hbcukid 2020년 11월 19일
Fairly new to MATLAB so I am just playing around to familiarize myself with it.
Lets say I have a 4x4 matrix, "A", how would I add for exaple 2 to every element in every other row (in this instance odd rows)? How would this differ if I wanted it for every nth row?
A = [1 1 0 0; 1 1 0 0; 0 0 0 0; 0 0 0 0]
desired output:
A = [3 3 2 2; 1 1 0 0; 2 2 2 2; 0 0 0 0]

채택된 답변

James Tursa
James Tursa 2020년 11월 19일
편집: James Tursa 2020년 11월 19일
A(k,:) is the k'th row of A
A(:,k) is the k'th column of A
A([k m p],:) is the sub-matrix formed from the k'th, m'th, and p'th rows of A
A(:,[k m p]) is the sub-matrix formed from the k'th, m'th, and p'th columns of A
A(1:5:end,:) is the sub-matrix formed from every 5'th row of A
A(:,1:5:end) is the sub-matrix formed from every 5'th column of A
etc.
To add a number to such sub-matrices, simply put them in an assignment
A(whatever) = A(whatever) + number;
The "whatever" is the indexing you want to pick off rows or columns etc.
  댓글 수: 1
hbcukid
hbcukid 2020년 11월 19일
Thank you so much, it worked! Still a little confused on the
A([k m p],:) is the sub-matrix formed from the k'th, m'th, and p'th rows of A
A(:,[k m p]) is the sub-matrix formed from the k'th, m'th, and p'th columns of A
part but will try to do additional reading

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by