accessing vector elements in random order and update new values

1 次查看(过去 30 天)
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 中查找有关 Numeric Types 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by