Inserting a row into the matrix in each iterations
    4 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    Mark Loui
 le 18 Mar 2021
  
    
    
    
    
    Commenté : Juan Diego Castillo Cruz
 le 19 Mar 2021
            Hi there i like to ask how can i insert a new row/colum into the matrix with each increasing of the iterations
for i=1:n-1
    h(i)=x(i+1)-x(i);
    iter_h=iter_h+1;
    Dia_1= [1 h(i) h(i+1)]     
end
My interest is to add one coloum into Dia_1 for each iterations, for example Dia_1=[1 h(1) h(2) h(3) and so on for every new i, the h(i) will pop up here 
How can i do so?
6 commentaires
  Adam Danz
    
      
 le 18 Mar 2021
				h = diff(x);
  Rik
      
      
 le 18 Mar 2021
				That is indeed what I meant: if you want to calculate x(i+1)-x(i) for all positions of x, then you can simply do diff(x) to get the same result.
I have no clue what you are trying to achieve, so I can't give you a real solution. What do you want to do with Dia_1?
If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks).
Réponse acceptée
  Juan Diego Castillo Cruz
 le 18 Mar 2021
        
      Modifié(e) : Juan Diego Castillo Cruz
 le 18 Mar 2021
  
      I understand that you want to make the variable grow every iteration. Then:
Dia_1=[]; %Create a array
for i=1:n-1
    h(i)=x(i+1)-x(i);
    iter_h=iter_h+1;
    Dia_1 = [Dia_1 h(i)] %This insert columns
end
4 commentaires
  Rik
      
      
 le 19 Mar 2021
				Did you already do a basic Matlab tutorial? It sounds like that would be a good time investment.
  Juan Diego Castillo Cruz
 le 19 Mar 2021
				I assume that h is a matrix then:
h = ones(m,n-1);
Dia_1 = ones(m,n)
for i=1:n-1
    h(:,i) = [i 2*i 3*i];   %For example this vector
    Dia_1(:,i+1)=h(:,i).*Dia_1(:,i);
end
This was my understanding.
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Matrix Indexing 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!



