Matrix operation connecting two Matrix
1 view (last 30 days)
Show older comments
Pretesh John
on 17 Nov 2020
Commented: Pretesh John
on 27 Nov 2020
I have two matrices A=[11;15;45;17;1] B=[2 4 0; 3 4 5; 4 0 0; 5 0 0; 0 0 0] Where B is a kind of refrence matrix which is showing the element number of A. For example B11 (=2) means second element of A (=15), B22 (=4) means forth element of A (=17) I want to create a mathematical expression like X=A+u(B-A) where 'u' is a constant number and 'X' is also and column matrix containing all the values of expression. Example: for first row of B X11=A11+u(B11-A11)=A11+u(A21-A11) X21=A11+u(B12-A11)=A11+u(A41-A11) Then X31=A21+u(B21-A21)=A21+u(A31-A21) and so on
How can I create a program. Thanks
0 Comments
Accepted Answer
CHENG QIAN LAI
on 24 Nov 2020
Edited: CHENG QIAN LAI
on 24 Nov 2020
For example:
B(1,1)=2 means second element of A (=15) --> A( B(1,1),1 ) = A( 2,1 ) = 15
B(2,2)=4 means forth element of A (=17) --> A( B(2,2),1 ) = A( 4,1 ) = 17
B(1,3)=0 --> A( B(1,3),1 ) = A( 0,1 ) -->Index in position 1 is invalid. Array indices must be positive integers or logical values.
if B(r,c)=0 ,set X(r,c)=NaN
Example:
let u=1;
for first row of B
X(1,1)=A(1,1)+u*( A(B(1,1),1) - A(1,1) )=A(1,1)+ u*( A(2,1) -A(1,1) )=15
X(1,2)=A(1,1)+u*( A(B(1,2),1) - A(1,1) )=A(1,1)+ u*( A(4,1) -A(1,1) )=17
X(1,3)=A(1,1)+u*( A(B(1,3),1) - A(1,1) )=A(1,1)+ u*( A(0,1) -A(1,1) ) -->NaN
for secon row of B
X(2,1)=A(2,1)+u*( A(B(2,1),1) - A(2,1) )=A(2,1) +u*( A(3,1) -A(2,1) )=45
X(2,2)=A(2,1)+u*( A(B(2,2),1) - A(2,1) )=A(2,1) +u*( A(4,1) -A(2,1) )=17
X(2,3)=A(2,1)+u*( A(B(2,3),1) - A(2,1) )=A(2,1) +u*( A(5,1) -A(2,1) )=1
for third row of B
X(3,1)=A(3,1)+u*( A(B(3,1),1) - A(3,1) )=A(3,1) +u*( A(4,1) -A(3,1) ) =17
X(3,2)=A(3,1)+u*( A(B(3,2),1) - A(3,1) )=A(3,1) +u*( A(0,1) -A(3,1) ) -->NaN
X(3,3)=A(3,1)+u*( A(B(3,3),1) - A(3,1) )=A(3,1) +u*( A(0,1) -A(3,1) ) -->NaN
row=4
X(4,1)=A(4,1)+u*( A(B(4,1),1) - A(4,1) )=A(4,1) +u*( A(5,1) -A(4,1) )=29
X(4,2)=A(4,1)+u*( A(B(4,2),1) - A(4,1) )=A(4,1) +u*( A(0,1) -A(4,1) ) -->NaN
X(4,3)=A(4,1)+u*( A(B(4,3),1) - A(4,1) )=A(4,1) +u*( A(0,1) -A(4,1) ) -->NaN
row=5
X(5,1)=A(3,1)+u*( A(B(5,1),1) - A(3,1) )=A(3,1) +u*( A(0,1) -A(5,1) ) -->NaN
X(5,2)=A(3,1)+u*( A(B(5,2),1) - A(3,1) )=A(3,1) +u*( A(0,1) -A(5,1) ) -->NaN
X(5,3)=A(3,1)+u*( A(B(5,3),1) - A(3,1) )=A(3,1) +u*( A(0,1) -A(5,1) ) -->NaN
u=1;
A=[11;15;45;17;1];
B=[2 4 0;
3 4 5;
4 0 0;
5 0 0;
0 0 0];
X=NaN(size(B)) % =NaN(5,3)
% X =
% NaN NaN NaN
% NaN NaN NaN
% NaN NaN NaN
% NaN NaN NaN
% NaN NaN NaN
a=repmat(A,1,3)
% a =
% 11 11 11
% 15 15 15
% 45 45 45
% 17 17 17
% 1 1 1
idx=B>0 & B <=numel(A) % or idx=find( B>0 & B <=numel(A) )
% idx =
% 5×3 logical array
% 1 1 0
% 1 1 1
% 1 0 0
% 1 0 0
% 0 0 0
X(idx)=a(idx) + u*( a(B(idx),1) - a(idx) )
% X =
% 15 17 NaN
% 45 17 1
% 17 NaN NaN
% 1 NaN NaN
% NaN NaN NaN
% or
Y=a(idx) + u*( a(B(idx),1) - a(idx) )
% Y =
% 15
% 45
% 17
% 1
% 17
% 17
% 1
More Answers (0)
See Also
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!