For loop element Matrix construction

1 回表示 (過去 30 日間)
Mark
Mark 2012 年 11 月 13 日
I am currently writing an element based analytical program for Uni but have hit a wall. I have a for loop for a set number of elements within which a number of different equations each giving a different aspect of each element. So, for 10 iterations(i) (or 10 elements) i am calculating A(i) B(i) C(i) etc. and i want to put these in one table or matrix related to their element number so the colums are denoted by the element number and the rows have a diferent result in (A,B or C...). What code do i use to do this?

採用された回答

Thomas
Thomas 2012 年 11 月 13 日
編集済み: Thomas 2012 年 11 月 13 日
This video should help:
You can save the output in a vector or matrix as shown,
Eg:
% Preallocate space for y
y = zeros(1,10);
z = zeros(1,10);
for ii=1:10
y(ii)=ii+rand; % use y(ii) so that it is written as a vector
z(ii)=ii+rand; % use z(ii) so that it is written as a vector
end
out=[y' z'] % output as a matrix

その他の回答 (2 件)

Mark
Mark 2012 年 11 月 13 日
編集済み: Mark 2012 年 11 月 13 日
I dont quite see how to do it for numerous varibales...Sorry :-s
So my code is:
for i = 1:nbe;
r(i) = (R*xo)+(deltar*(i-1));
x(i) = r(i)/R;
M(i) = x(i)*((Omega*R)/sos);
Theta(i) = Trad0+(x(i)*Trad1);
a(i) = (0.1/(1-M(i)^2))-(0.01*M(i));
v1(i) = ((Omega*a(i)*R*sigma)/16)*(-1+(1+((32*Theta(i)*x(i))/(a(i)*sigma)))^0.5);
phi(i) = atan((v1(i))/(Omega*r(i)));
end
And i want a table or matrix in the form of i against all these variables, one per row...
  2 件のコメント
Jan
Jan 2012 年 11 月 13 日
I still do not understand the question. Where are "A, B, C, ..." created in your code? Why do you need a loop here? A vectorization would simplify the code.
In general creating "A, B, C, ..." is less efficient than creating a cell "A{1}, A{2}, ...", because the later can processed in a loop directly.
Mark
Mark 2012 年 11 月 13 日
Sorry, the initial question was an example. I used a for loop because when i tried creating it to solve element wise outside of a loop i started getting inner matrix dimensionality issues. Would you suggest a different approach?

サインインしてコメントする。


Mark
Mark 2012 年 11 月 13 日
Sorry to be so confusing, ill leave it and do it a slightly different way, thanks though

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by