mat1(mat2) = mat1 assignment
Afficher commentaires plus anciens
How does the mat1(mat2) = mat1 assignment work given two one dimensional arrays like so:
t1 = [ 0 1 2 3 4 5 6 ];
t2 = [ 7 6 5 4 3 1 1 ];
t1(t2)=t1
What is the "pattern"? - and how would you implement something like this in C++/pseudo code?
1 commentaire
Hey!
Assuming by "Pattern" you want to understand the output.
here initially your array t1 = [ 0 1 2 3 4 5 6 ] and t2 = [ 7 6 5 4 3 1 1 ], Since MATLAB has 1 based indexing, it is storing the values in t1 based on the indexing order mentioned in t2
Let's assume your current values of t1 are stored in a temporary array temp = [ 0 1 2 3 4 5 6 ]
% in iteration = 1 , t2(1) = 7 => t1(7) = 0 {since temp(1) = 0} => t1 = [ 0 1 2 3 4 5 0 ]
% in iteration = 2 , t2(2) = 6 => t1(6) = 1 {since temp(2) = 1} => t1 = [ 0 1 2 3 4 1 0 ]
% in iteration = 3 , t2(3) = 5 => t1(5) = 2 => t1 = [ 0 1 2 3 2 1 0 ]
% in iteration = 4 , t2(4) = 4 => t1(4) = 3 => t1 = [ 0 1 2 3 2 1 0 ]
% in iteration = 5 , t2(5) = 3 => t1(3) = 4 => t1 = [ 0 1 4 3 2 1 0 ]
% in iteration = 6 , t2(6) = 1 => t1(1) = 5 => t1 = [ 5 1 4 3 2 1 0 ]
% in iteration = 7 , t2(7) = 1 => t1(1) = 6 => t1 = [ 6 1 4 3 2 1 0 ]
%you can consider this as a pseudo code that can be reproduced in your
%desired language
t1 = [ 0 1 2 3 4 5 6 ];
t2 = [ 7 6 5 4 3 1 1 ];
temp = t1 ;
for i = 1:size(temp,2) %for loop of the size of array temp or t1
t1(t2(i)) = temp(i);
end
t1
Réponse acceptée
Plus de réponses (1)
Hey!
Assuming by "Pattern" you want to understand the output.
here initially your array t1 = [ 0 1 2 3 4 5 6 ] and t2 = [ 7 6 5 4 3 1 1 ], Since MATLAB has 1 based indexing, it is storing the values in t1 based on the indexing order mentioned in t2
Let's assume your current values of t1 are stored in a temporary array temp = [ 0 1 2 3 4 5 6 ]
% in iteration = 1 , t2(1) = 7 => t1(7) = 0 {since temp(1) = 0} => t1 = [ 0 1 2 3 4 5 0 ]
% in iteration = 2 , t2(2) = 6 => t1(6) = 1 {since temp(2) = 1} => t1 = [ 0 1 2 3 4 1 0 ]
% in iteration = 3 , t2(3) = 5 => t1(5) = 2 => t1 = [ 0 1 2 3 2 1 0 ]
% in iteration = 4 , t2(4) = 4 => t1(4) = 3 => t1 = [ 0 1 2 3 2 1 0 ]
% in iteration = 5 , t2(5) = 3 => t1(3) = 4 => t1 = [ 0 1 4 3 2 1 0 ]
% in iteration = 6 , t2(6) = 1 => t1(1) = 5 => t1 = [ 5 1 4 3 2 1 0 ]
% in iteration = 7 , t2(7) = 1 => t1(1) = 6 => t1 = [ 6 1 4 3 2 1 0 ]
%you can consider this as a pseudo code that can be reproduced in your
%desired language
t1 = [ 0 1 2 3 4 5 6 ];
t2 = [ 7 6 5 4 3 1 1 ];
temp = t1 ;
for i = 1:size(temp,2) %for loop of the size of array temp or t1
t1(t2(i)) = temp(i);
end
t1
Catégories
En savoir plus sur Language Fundamentals dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!