For each loop upon fixed size array

조회 수: 33 (최근 30일)
Jimmy cho
Jimmy cho 2020년 11월 16일
댓글: Jimmy cho 2020년 11월 16일
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 .

채택된 답변

David Hill
David Hill 2020년 11월 16일
for k=1:100
A=A+1;
end
  댓글 수: 4
Jimmy cho
Jimmy cho 2020년 11월 16일
Can't I do that in matlab? for instance in python ..and the reason is that foreach it's more elegant that using idexes.
another question in the context, lets assume I have array A=[x y z ] and x , y , z as I said in my thread above is integer values, how I pass/run over A array by foreach in matlab code?
just like this:
for m:A
%do something with m / do arithmatic with m , m is the value of array A and next value of array A updated to m at each iteration
end
Image Analyst
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
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.
  댓글 수: 2
Jimmy cho
Jimmy cho 2020년 11월 16일
" Acopy = A + k " in this statement the A is changed at every iteration to the next value organizedly ? I mean first value at 1, then next iteration value at 2 , then next iteration value at 3 ?
Jimmy cho
Jimmy cho 2020년 11월 16일
If I understand you well, if I do this:
for k = 1 : 100
m = A ;
end
so every value of A will be copied / stored to m correspondingly , yes?
Implicitly A is going to the next value at every iteration

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by