accessing vector elements in random order and update new values

1 Ansicht (letzte 30 Tage)
jano satti
jano satti am 14 Apr. 2017
Beantwortet: KSSV am 17 Apr. 2017
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

Antworten (1)

KSSV
KSSV am 17 Apr. 2017
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

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by