How do I create a for loop in MATLAB for this?

Greeting everyone.
I have a these matrix: A is by the order of 10 by 3.
I want to do this calculation: B = (A-min(A))/[max(A)-min(A)]
After that I want B to be by the order of 10 by 3.
Is there a way where I can do a loop of this?

1 comentario

Walter Roberson
Walter Roberson el 30 de Mzo. de 2020
Sure. You can deliberately create a loop or two to find the max and min, and you can deliberately create a loop or two to rescale. You do not need to do those things, but you can.
The trick for the vectorized form is that by default max and min work over only one dimension of a multidimensional array. See the documentation for how to change that to process all dimensions.

Iniciar sesión para comentar.

Respuestas (2)

James Tursa
James Tursa el 30 de Mzo. de 2020
Editada: James Tursa el 30 de Mzo. de 2020
If you want to use the overall min and max, then just use A(:) for the calculations. E.g.,
B = (A-min(A(:)))/[max(A(:))-min(A(:))];
A(:) is shorhand for reshaping the variable into a column vector.

6 comentarios

Chloe Sim
Chloe Sim el 30 de Mzo. de 2020
Hello, thank you for the quick response. But how do I get the individual values and store into a matrix of 10 rows and 3 columns?
James Tursa
James Tursa el 30 de Mzo. de 2020
B will be the same size as A. Did you try the code?
Chloe Sim
Chloe Sim el 28 de Abr. de 2020
Hello, yes i tried the code. But i needed the max and min of each column, not the entire matrix.
Walter Roberson
Walter Roberson el 28 de Abr. de 2020
Are you still required to do a loop for it? (If so, then go ahead and write a loop)
Chloe Sim
Chloe Sim el 29 de Abr. de 2020
Actually, I was guessing using loop will be the best option in the case. But I not sure, how to go about and do it.
Walter Roberson
Walter Roberson el 29 de Abr. de 2020
You can find the per-row min using min(A, [], 2) and you can find the per-row max using similar code. You can do the subtractions . But you should change the division from / to ./
The resulting expression should handle the entire matrix with no loops.

Iniciar sesión para comentar.

Steven Lord
Steven Lord el 30 de Mzo. de 2020

1 voto

Use the normalize function with the 'range' method. No for loop is required.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 30 de Mzo. de 2020

Comentada:

el 29 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by