mat1(mat2) = mat1 assignment

5 views (last 30 days)
Andreas Utkilen
Andreas Utkilen on 22 Jun 2022
Answered: yatharth on 27 Jun 2022
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
t1 = 1×7
6 1 4 3 2 1 0
What is the "pattern"? - and how would you implement something like this in C++/pseudo code?
  1 Comment
yatharth
yatharth on 22 Jun 2022
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
t1 = 1×7
6 1 4 3 2 1 0

Sign in to comment.

Accepted Answer

Jan
Jan on 22 Jun 2022
The pattern is trivial. I do not understand, what is unclear:
t1 = [ 0 1 2 3 4 5 6 ];
t2 = [ 7 6 5 4 3 1 1 ];
t1(t2) = t1;
% Explicitly:
t1([7 6 5 4 3 1 1]) = [0 1 2 3 4 5 6];
The 7th element of t1 is set to 0, the 6th to 1, the 5th to 2 and so on. That t1 appears on the left and on the right side does not matter in any way. It is performed like:
tmp = zeros(1, 7);
tmp(t2) = t1;
t1 = tmp;

More Answers (1)

yatharth
yatharth on 27 Jun 2022
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
t1 = 1×7
6 1 4 3 2 1 0

Community Treasure Hunt

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

Start Hunting!

Translated by