How can I perform matrix calculations within the own matrix?

1 次查看(过去 30 天)
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