Effacer les filtres
Effacer les filtres

how to generate a matrix with 2 columns and changeable number of rows

2 vues (au cours des 30 derniers jours)
LB
LB le 22 Fév 2019
Commenté : Star Strider le 23 Fév 2019
Hello
I am new to the MATLAB and I need help
I want to generate a matrix with 2 columns and changeable number of rows according to user input. each number in the matrix will be more than the previous one with a certain number define by user. (in the example below I used 0.4)
here is the output that I am trying to obtain
[ 0 0
0.4 0
0.8 0
1.2 0
0 0.4
0.4 0.4
0.8 0.4
1.2 0.4
0 0.8
0.4 0.8
0.8 0.8
1.2 0.8
0 1.2
0.4 1.2
0.8 1.2
1.2 1.2 ]
any suggestions ??

Réponse acceptée

Star Strider
Star Strider le 22 Fév 2019
use the meshgrid (link) function (or optionally ndgrid (link)):
step = 0.4;
v = 0 :step : 3*step;
[X,Y] = meshgrid(v);
OutMg = [Y(:), X(:)];
step = 0.4;
v = 0 :step : 3*step;
[X,Y] = ndgrid(v);
OutND = [X(:), Y(:)];
Choose the function that most closely approximates what youwant to do. Here, they both produce the same result.
  2 commentaires
LB
LB le 23 Fév 2019
thank you i used ndgrid and it is exactly what i need
Star Strider
Star Strider le 23 Fév 2019
As always, my pleasure.

Connectez-vous pour commenter.

Plus de réponses (2)

Steven Lord
Steven Lord le 22 Fév 2019
Call the meshgrid function and reshape the outputs from meshgrid into column vectors.

Walter Roberson
Walter Roberson le 22 Fév 2019
[C1, C2] = ndgrid((0:num_increments-1) * increment_value);
output = [C1(:), C2(:)];

Catégories

En savoir plus sur Particle & Nuclear Physics dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by