Looping on one line

29 views (last 30 days)
Patrick on 7 Dec 2015
Edited: Stephen23 on 19 Jun 2019
I searched the forums and couldn't find anything like this. So either I am doing it completely wrong or there is another method to accomplish it. Here is the code
if true
% code
SpearMeanRhoAll = vertcat(SpearMeanRhoP2,SpearMeanRhoP3,SpearMeanRhoP4,SpearMeanRhoP5,SpearMeanRhoP6,SpearMeanRhoP7,SpearMeanRhoP8,SpearMeanRhoP9,SpearMeanRhoP10,SpearMeanRhoP11,SpearMeanRhoP12,SpearMeanRhoP13,SpearMeanRhoP14,SpearMeanRhoP15,SpearMeanRhoP16,SpearMeanRhoP17,SpearMeanRhoP18,SpearMeanRhoP19,SpearMeanRhoP20,SpearMeanRhoP21);
As you can see the variable "SpearMeanRhoAll" is composed of SpearMeanRhoPx where x goes from 2 to 21. Does anyone know how to loop like this on one line?
Thanks for any help.
  1 Comment
Adam on 7 Dec 2015
How to do what in a loop? Loops are not generally meant to be done in one line other than loop-replacement functions like arrayfun.
In general you should never put yourself in a situation where you have 21 variables with names like that in the first place. Your SpearMeanRhoAll representation would be far better to be used right from the start.

Sign in to comment.

Accepted Answer

Stephen23 on 7 Dec 2015
Edited: Stephen23 on 19 Jun 2019

More Answers (0)


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

Community Treasure Hunt

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

Start Hunting!