Desired array as output; concatenation of 3 N X 1 variables into one N X 3 variable using loop.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Subhash Chandra Ranga
el 28 de Abr. de 2021
Comentada: Subhash Chandra Ranga
el 28 de Abr. de 2021
i have three variables with values as bellow
S = 100 X 1 double
T = 100 X 1 double
Ri = 100 X 1 double
i want make an output varaiable with these three variables as a 100 X 3
mpoints = [];
for i= 1:100
if i < 101
mpoints = [S(i) T(i) depth(i)];
i = i+1;
end
end
I have tried the above code the loop should itterate 100 times but instead, it outputs only 1 X 3 intead of 100 X 3. Please suggest me where i did wrong.
0 comentarios
Respuesta aceptada
David Fletcher
el 28 de Abr. de 2021
Editada: David Fletcher
el 28 de Abr. de 2021
You are overwriting mpoints on every iteration, so you will end up with only the last set of values. Try:
mpoints = [];
for i= 1:100
mpoints(i,:) = [S(i) T(i) depth(i)];
end
You don't need to increment i every iteration - the loop handles it. The if statement is also largely pointless in this context at least.
You could of course just write mpoints=[S T depth] to achieve the same thing
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!