How can I perform matrix calculations within the own matrix?

1 vue (au cours des 30 derniers jours)
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.

Réponse acceptée

Antonio Morales
Antonio Morales le 4 Jan 2017
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

Plus de réponses (1)

Greg
Greg le 4 Jan 2017
Modifié(e) : Greg le 4 Jan 2017
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 commentaire
Antonio Morales
Antonio Morales le 4 Jan 2017
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by