How to sample values from inner for loop vector to an outer for loop vector
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Trond Oesten
el 12 de Feb. de 2015
Comentada: Trond Oesten
el 12 de Feb. de 2015
Hi,
I'm trying to model the difference of strength in a cable consisting of N wires in a cable due to different strain values. For this I have created two for loops. In the inner for loop I calculate the strength of the cables for each strain step. These values are sampled in a vector. I want to use Monte Carlo simulations and run the inner for loop K times. For each simulation of K I want to sample the "jobb" vector into a large vector containing every "jobb" vector value. Can this be done?
my script:
clc; clear all; close all;
E = 7.75*1e4;
Area = 38.48;
mu = 0.4343;
su = 0.3295;
my = 0.8686;
sy = 0.1318;
k = 8;
N = 200;
K = 10;
for simulations = 1:K;
P = rand(N,1);
epsilon_y = logninv(P,my,sy);
epsilon_u_y = logninv(P,mu,su);
epsilon_u = (epsilon_y + epsilon_u_y);
n = numel(P);
g = 1:k;
epsilon_0 = 1;
jobb = zeros(k,1);
model = zeros(n,1);
for epsilon = 1:k;
for ii = 1:n;
if epsilon_y(ii) >= epsilon
ep = epsilon;
elseif epsilon_y(ii) < epsilon && epsilon < epsilon_u(ii)
ep = epsilon_y(ii);
elseif epsilon_u(ii) <= epsilon
ep = 0;
end
model(ii) = ((ep/100)*E);
end
p = sum(model)*Area;
jobb(epsilon) = p;
end
large_vector_that_I_want_to_find = ..;
end
Best regards
Trond Oesten
0 comentarios
Respuesta aceptada
Guillaume
el 12 de Feb. de 2015
Rather than storing the result in a big vector, I'd use a matrix, where each column is a simulation:
jobb = zeros(k, K);
for simulation = 1:K
%initialise everything but jobb as normal
for epsilon = 1:k
%calculate everything as normal
jobb(epsilon, simulation) = p;
end
end
If you want to convert the matrix into a big vector, then it's simply
large_vector_that_I_want_to_find = jobb(:);
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!