For each loop upon fixed size array
조회 수: 33 (최근 30일)
이전 댓글 표시
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
채택된 답변
David Hill
2020년 11월 16일
for k=1:100
A=A+1;
end
댓글 수: 4
Image Analyst
2020년 11월 16일
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.
추가 답변 (1개)
Image Analyst
2020년 11월 16일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!