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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by