How to generalize this code?

I'm working with WDS (Water Distribution System) at the moment I'm using a small one so I can write every matrix manually... but must exist something easyer! Example: P is matrix of pressure:
P1=[P1(:,2),P1(:,1),P1(:,3),P1(:,4),P1(:,5),P1(:,6)];
P2=[P2(:,2),P2(:,1),P2(:,3),P2(:,4),P2(:,5),P2(:,6)];
P3=[P3(:,2),P3(:,1),P3(:,3),P3(:,4),P3(:,5),P3(:,6)];
P4=[P4(:,2),P4(:,1),P4(:,3),P4(:,4),P4(:,5),P4(:,6)];
P5=[P5(:,2),P5(:,1),P5(:,3),P5(:,4),P5(:,5),P5(:,6)];
P6=[P6(:,2),P6(:,1),P6(:,3),P6(:,4),P6(:,5),P6(:,6)];
P7=[P7(:,2),P7(:,1),P7(:,3),P7(:,4),P7(:,5),P7(:,6)];
P8=[P8(:,2),P8(:,1),P8(:,3),P8(:,4),P8(:,5),P8(:,6)];
P9=[P9(:,2),P9(:,1),P9(:,3),P9(:,4),P9(:,5),P9(:,6)];
P0=[P0(:,2),P0(:,1),P0(:,3),P0(:,4),P0(:,5),P0(:,6)];
How can I write this code for N matrix of pressure?

5 Comments

Stephen23
Stephen23 on 22 Feb 2016
Edited: Stephen23 on 22 Feb 2016
The simplest and best answer is "do not create variables dynamically". That is a totally Lunatix way of programming, and should be avoided.
If you tell us exactly what you are trying to achieve then we can show you better, neater and robuster ways of doing it.
I need to create N matrix, I want to call the first matrix P1 the second P2 until P'N'. somethin like this:
N=10
For i=1:N
P(i)=[1,5,9]
end
I would like to have back
P1=[1,5,9]
P2=[1,5,9]
...
P10=[1,5,9]
@Lunatix: that is exactly what you should not do. Read my answer to know why.
I'm sorry, now I understand what you told me before! I'm trying to use cell array... I'm not a programmer and I use matlab as a calculator! PS I'm going to translate my work using cell array than, maybe, I can show you my code! And you can tell me if it is the right way...
Stephen23
Stephen23 on 22 Feb 2016
Edited: Stephen23 on 22 Feb 2016
Using a cell array is a great idea. If you have any questions or want a clarifications then create a new question (rather than continuing this thread).

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 22 Feb 2016

Edited:

on 19 Jun 2019

Community Treasure Hunt

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

Start Hunting!