How can I perform matrix calculations within the own matrix?

조회 수: 1 (최근 30일)
Antonio Morales
Antonio Morales 2017년 1월 4일
답변: Antonio Morales 2017년 1월 4일
Hi,
I have a 9x612 matrix (A). It consists of 102 variables measured 6 times each (612 columns), in 9 different situations (9 rows). I would like to normalize each repetition to its first one (i.e. A(:,1:6) normalised to A(:,1); A(:,7:12) normalised to A(:,7), etc.
How could I perform these calculations using a for loop?
This is what I have been trying so far without success:
A_normalised = zeros(size(A));
for ii = 1:6:length(A);
for kk = ii+1:ii+5;
A_normalised(:,:) = ((A(:,kk).*100)./A(:,ii));
end
end
However this leads to the error: "Assignment has fewer non-singleton rhs dimensions than non-singleton subscripts"
Please, find attached a matrix example.

채택된 답변

Antonio Morales
Antonio Morales 2017년 1월 4일
A_normalised = zeros(size(A));
for ii = 1:6:length(A)
for kk = ii:ii+5
A_normalised(:,kk) = (A(:,kk).*100)./ A(:,ii)-100;
end
end

추가 답변 (1개)

Greg
Greg 2017년 1월 4일
편집: Greg 2017년 1월 4일
The right hand side (rhs) evaluates to a column. The left hand side (lhs) references the entire matrix (612 columns). You can't stick a single column into 612 separate columns (with that type of index notation).
On the lhs, specify the proper column to store the result in. (Hint: it's probably one of your loop index variables).
Also, I would use repmat, repelem, bsxfun, or other similar function to remove both loops entirely.
  댓글 수: 1
Antonio Morales
Antonio Morales 2017년 1월 4일
Thanks. This is now giving me what I was looking for:
A_normalised = zeros(size(A));
for ii = 1:6:length(A)
for kk = ii:ii+5
A_normalised(:,kk) = (A(:,kk).*100)./ A(:,ii)-100;
end
end

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by