I have the code below, and I want to store the variable "vetor_momentoj" returned from the for loop in terms of the indexes i and j. In other words, in each interaction i, I will have j (10) values of the variable "vetor_momentoj". Similarly, I want to store the variable "jvals". In each interaction i it's assuming a vector of values as declared with linspace. Can anyone help me, please?
it = 10000;
jvals = zeros(it,1);
vetor_momentoj = zeros(it,1);
for i = 1:it
jvals(i) = linspace(0, 0.03, 10);
for j = 1:numel(jvals)
curvatura = jvals(j);
funj = @dissertacao_curvatura_funcao;
x0j = 1;
xj = fzero(funj,x0j);
vetor_momentoj(j) = momentoj;
end
end

 採用された回答

per isakson
per isakson 2021 年 5 月 16 日
編集済み: per isakson 2021 年 5 月 16 日

0 投票

"store the variable "vetor_momentoj" [...] in terms of the indexes i and j."
Something like this? I replaced the calculation of momentoj by rand to be able to run the script.
There is no reason to have the statement jvals = linspace(0, 0.03, 10); inside the loop. The value of jvals doesn't change.
%%
it = 10000;
jvals = linspace(0, 0.03, 10);
vetor_momentoj = nan( numel(jvals), it );
for i = 1:it
for j = 1:numel(jvals)
curvatura = jvals(j);
% funj = @dissertacao_curvatura_funcao;
x0j = 1;
% xj = fzero(funj,x0j);
vetor_momentoj(j,i) = rand; % momentoj;
end
end
whos vetor_momentoj
Name Size Bytes Class Attributes vetor_momentoj 10x10000 800000 double
"store the variable "jvals"." I don't see why. The statement jvals(i) = linspace(0, 0.03, 10); throws an error. repmat( linspace(0,0.03,10), .... ) outside the script, isn't that good enough?

4 件のコメント

Ana Carolina da Silva Pacheco
Ana Carolina da Silva Pacheco 2021 年 5 月 16 日
Thank you! But the variable jvals has to be inside the loop. The value 0.03 I put here is hypotetical, in the real code it's a value that depends on the index i. So how should I proceed?
Stephen23
Stephen23 2021 年 5 月 16 日
it = 10000;
nc = 10;
jvals = nan(it,nc);
vetor_momentoj = zeros(it,1);
for ii = 1:it
V = linspace(0, 0.03, nc);
for jj = 1:numel(V)
curvatura = V; % this variable is unused
funj = @dissertacao_curvatura_funcao; % why redefine this on every iteration?
x0j = 1; % why redefine this on every iteration?
xj = fzero(funj,x0j);
vetor_momentoj(jj) = momentoj; % where is MOMENTOJ defined?
end
jvals(ii,:) = V;
end
per isakson
per isakson 2021 年 5 月 17 日
Did you find a solution?
Ana Carolina da Silva Pacheco
Ana Carolina da Silva Pacheco 2021 年 5 月 17 日
Yes, thank you!!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by