For each loop upon fixed size array
34 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jimmy cho
el 16 de Nov. de 2020
Comentada: Jimmy cho
el 16 de Nov. de 2020
Hi guys,
I have three variable x y z that are integer values and for instance its values are :
x=5 , y=2 , z=3;
and I have an array called A equal to A=[x y z]
I want to do for each loop upon the array A and adding +1 for every iteration of the for each loop .
I came from world of c world and rarely I use matlab (it depends on what do I want to implement ) .. how can I do that in matlab?
thanks alot for any help .
0 comentarios
Respuesta aceptada
David Hill
el 16 de Nov. de 2020
for k=1:100
A=A+1;
end
4 comentarios
Image Analyst
el 16 de Nov. de 2020
Well most MATLABers would think
result = diff(A) > 0;
is more elegant than using for or foreach. The diff() function subtracts the kth element from the (k+1)st element.
result is a logical matrix giving essentially a "map" of what elements are greater than the prior element.
Más respuestas (1)
Image Analyst
el 16 de Nov. de 2020
Another option
Acopy = A; % Make copy of original
for k = 1 : 100
Acopy = A + k;
end
The original A does not get changed, however Acopy still gets overwritten at each iteration.
2 comentarios
Ver también
Categorías
Más información sobre Function Creation 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!