1 view (last 30 days)

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!

Bruno Luong
on 14 Jul 2019

Edited: Bruno Luong
on 15 Jul 2019

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)

Sign in to comment.

KALYAN ACHARJYA
on 14 Jul 2019

Edited: 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

Image Analyst
on 14 Jul 2019

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.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.