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
- 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]
- 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]
- 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
0 个评论
回答(1 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!