MATLAB Answers

Krassor
0

Replace elements of a matrix by a smaller matrix which contains circular shaped values margins

Asked by Krassor
on 14 Jul 2019
Latest activity Commented on by Krassor
on 15 Jul 2019
Hello,
I'm having problems to efficiently replace some values of a result matrix.
The process is taking place ~50k times, so its very time intensive (result matrix: m,n > 1000).
Each time a much smaller matrix replaces a corresponding area in the result matrix.
The problem is, that the values in the small matrix form "the shape of a circle" and represent the presence of an attribute.
Like this:
A = result matrix (in reality: much bigger):
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
B = small matrix (in reality: more elements in matrix, so that the value margins form the shape of a circle):
0 0 0 0
0 5 5 0
0 5 5 0
0 0 0 0
By performing "A(1:4, 1:4) = B" I get the result C:
0 0 0 0 1
0 5 5 0 1
0 5 5 0 1
0 0 0 0 1
1 1 1 1 1
I want the result:
1 1 1 1 1
1 5 5 1 1
1 5 5 1 1
1 1 1 1 1
1 1 1 1 1
I managed to get the result by first deleting the specific area of the result to "0" (only the ones that are not "0" in the small matrix "B") and then adding only the values of the small matrix that are not "0" by using two for-loops element-wise (50k times).
Unfortunately this takes very long....
Is there a more efficient way to do so?
Thank you very much in advance!

  0 Comments

Sign in to comment.

3 Answers

Answer by Bruno Luong
on 14 Jul 2019
Edited by Bruno Luong
on 15 Jul 2019
 Accepted Answer

C = A;
sB = size(B);
idx = {1+(1:sB(1)),1+(1:sB(2))}; % adjust offset 1 and 1 if B to be moved at other place
C(idx{:}) = B
or depends on desired result
C(idx{:})= max(C(idx{:}), B)

  0 Comments

Sign in to comment.


Answer by KALYAN ACHARJYA on 14 Jul 2019
Edited by KALYAN ACHARJYA on 14 Jul 2019

A=[1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1];
B=[0 0 0 0;0 5 5 0;0 5 5 0;0 0 0 0];
[r c]=find(B>1);
A(r,c)=B(r,c)
Result:
A =
1 1 1 1 1
1 5 5 1 1
1 5 5 1 1
1 1 1 1 1
1 1 1 1 1

  1 Comment

Krassor's "Answer" to Kalyan moved here (to be a "Comment") since it's not an answer to the original question.
Thank you for your answer!
At first that looked exactly like what I wanted...
But when implementing, the result didn't seem right:
For example:
A=[1 1 1 1 1 1 1;1 1 1 1 1 1 1;1 1 1 1 1 1 1;1 1 1 1 1 1 1;1 1 1 1 1 1 1; 1 1 1 1 1 1 1;1 1 1 1 1 1 1];
B=[0 5 0; 5 5 5;0 5 0]
[r, c]=find(B>1)
A(1+r,1+c)=B(r,c)
B(r,c)
results in:
B =
0 5 0
5 5 5
0 5 0
r =
2
1
2
3
2
c =
1
2
2
2
3
A =
1 1 1 1 1 1 1
1 0 5 0 1 1 1
1 5 5 5 1 1 1
1 0 5 0 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
B =
5 5 5 5 5
0 5 5 5 0
5 5 5 5 5
0 5 5 5 0
5 5 5 5 5
As you can see, matrix A contains "0"s again.
I also don't understand the output r, c of the find-function.
Neither the result of B(r, c) and why A replaces the original "B" matrix (3x3) and not the B(r,c) (5x5)...
I'm very confused right now :)

Sign in to comment.


Answer by Krassor
on 15 Jul 2019

By doing this, I just replace the area in "A" by "B" with all the "0"s...
This is what I try to avoid.
I tried the following:
A=[1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1];
B=[0 5 0; 5 5 5;0 5 0]
[r c]=find(B)
A(r(1):r(size(r)), c(1):c(size(c)))=B(r(1):r(size(r)), c(1):c(size(c)))
Result:
B =
0 5 0
5 5 5
0 5 0
r =
2
1
2
3
2
c =
1
2
2
2
3
A =
1 1 1 1 1
5 5 5 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Basically I think the "find"-approach is the right one... If I replace each element of the "rows" and "columns"-vector one by one (for example: A(r(1), c(1)) = B(r(1), c(1)) ... ) the result is perfect.
Only I want to avoid using a loop.

  6 Comments

Your last solution is replacing at the places where B > A.
What I want is, replacing at the places where B ~= 0.
You are right, I didn't specify that in first place....
The replacement of the places where B = 0 is not wanted.
[i,j,b] = find(B);
A(sub2ind(size(A),i+1,j+1)) = b
That looks like the solution!
Perfect!
Thank you! :)

Sign in to comment.