MATLAB Answers

0

Fit multidimensional array into other multidimensional matrix with changing index

Asked by Pascal Loohuis on 18 Sep 2019 at 7:49
Latest activity Commented on by Pascal Loohuis on 19 Sep 2019 at 0:51
Hi!
Let's say you have the matrix A= 4 x 4 x 2 and B=2 x 2 x 2. I want for A(:,:,1) to paste B(:,:,1)=[1 1; 1 1] onto A(:,:,1) such that
A(:,:,1)=[ 1 1 0 0;
1 1 0 0;
0 0 0 0;
0 0 0 0]
and I want for A(:,:,2) to paste B(:,:,2)=[2 2; 2 2] onto A(:,:,2) such that
A(:,:,1)=[ 0 0 0 0;
0 0 0 0;
0 0 2 2;
0 0 2 2]
without using a for loop. Is this possible?
Kind regards,
Pascal

  0 Comments

Sign in to comment.

1 Answer

Answer by KALYAN ACHARJYA on 18 Sep 2019 at 8:24
Edited by KALYAN ACHARJYA on 18 Sep 2019 at 8:34

Suppose, as per your question, considering random A and B
A=rand(4,4,2);
B=rand(2,2,2);
Now you want for A(:,:,1) to paste B(:,:,1)
Let's see
>>A(:,:,1)
0.4387 0.1869 0.7094 0.6551
0.3816 0.4898 0.7547 0.1626
0.7655 0.4456 0.2760 0.1190
0.7952 0.6463 0.6797 0.4984
A(:,:,1) is having size 4x4 and
>>B(:,:,1)
0.8143 0.9293
0.2435 0.3500
B(:,:,1) is having size 2x2
How do you going to replace?

  3 Comments

It's not as a replacement, it's more that certain values will be replaced by the values of B. Sorry for the misunderstanding.
certain values will be replaced by the values of B
Just an example:
>>A(:,:,1)
0.4387 0.1869 0.7094 0.6551
0.3816 0.4898 0.7547 0.1626
0.7655 0.4456 0.2760 0.1190
0.7952 0.6463 0.6797 0.4984
and
>>B(:,:,1)
0.8143 0.9293
0.2435 0.3500
Now you can replace the B(:,:,1) in any quadrant of A(:,:,1) ; 2x 2 area??
Is this??
That's right. But I want to extend it to a multidimensional A and B. Whereby the different 2 by 2 matrices of B are placed on different positions within matrices of A.

Sign in to comment.