accessing vector elements in random order and update new values
    5 views (last 30 days)
  
       Show older comments
    
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 Comments
Answers (1)
  KSSV
      
      
 on 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
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
