How can I perform matrix calculations within the own matrix?

1 visualización (últimos 30 días)
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.

Respuesta aceptada

Antonio Morales
Antonio Morales el 4 de En. de 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

Más respuestas (1)

Greg
Greg el 4 de En. de 2017
Editada: Greg el 4 de En. de 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 comentario
Antonio Morales
Antonio Morales el 4 de En. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by