accessing vector elements in random order and update new values

조회 수: 1 (최근 30일)
jano satti
jano satti 2017년 4월 14일
답변: KSSV 2017년 4월 17일
A=[2 3 5]
B=[1 6 4]
X=[0 50 110 0 45]
Y=[250 0 0 35 0 120]
A and B are indices of X and Y respectively. I want to access in order (2,1)(3,6)(5,4) such that
  1. if X(A)<Y(B) then X(A)-Y(B),(50-250) now X=[0 0 110 0 45] and Y=[200 0 0 35 0 120]
  2. if X(A)>Y(B) then X(A)-Y(B),(110-120) now X=[0 0 0 0 45] and y=[200 0 0 35 0 10]
  3. final answer is X=[0 0 0 0 10 ]| then Y=[200 0 0 10 0 10]
How can I do that? Thanks in advance.
for r=1:length(X)
if X(A)<Y(B)
Ynew=minus(Y(B),X(A))
Y(B)=Ynew
X(A)=0
Xnew=X(A)
Anew=X+1
else if X(A)>Y(B)
for r=1:length(b)
Xnew=minus(X(A),Y(B))
X(A)=Xnew
Y(B)=0
Ynew=Y(B)
Ynew=Y+1
end
end
end
end

답변 (1개)

KSSV
KSSV 2017년 4월 17일
A=[2 3 5] ;
B=[1 6 4] ;
X=[0 50 110 0 45] ;
Y=[250 0 0 35 0 120] ;
X0=X ;
Y0=Y ;
for i = 1:length(A)
if X(A(i))<Y(B(i))
Y(B(i))=Y(B(i))-X(A(i)) ;
X(A(i))=0 ;
elseif X(A(i))>Y(B(i))
Y(B(i))=Y(B(i))-X(A(i)) ;
X(A(i))=0 ;
end
end

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by