Putting Output values into a matrix.

Hi, I have an if statement within a double for loop. I would like to be able to put the two index values that satisfy the if statement into a matrix that I can then manipulate etc. Currently, I am just using "disp" to display the two index values. How can I go about putting the values into a matrix?

 Respuesta aceptada

Star Strider
Star Strider el 24 de Feb. de 2017
Editada: Star Strider el 24 de Feb. de 2017
One approach:
vals = [];
for k1 = 1:10
for k2 = 1:10
if k1 == k2 % Test Condition
vals = [vals; k1 k2]; % Store Index Values
end
end
end
Here, the ‘vals’ matrix will be:
vals =
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10

2 comentarios

Rashfa Baalfaqih
Rashfa Baalfaqih el 27 de Nov. de 2020
What if I want to put a title for each column?
Star Strider
Star Strider el 27 de Nov. de 2020
Use array2table and then add the variable (column header) names.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 24 de Feb. de 2017

Comentada:

el 27 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by