could anyone help me to get C from A and B
2 views (last 30 days)
Show older comments
If A =
1 2 3 4 5
11 12 13 14 15
6 7 8 9 10
21 22 23 24 25
26 27 28 29 30
B =
0 0 41 0 0
45 0 0 0 0
0 43 0 0 0
0 0 0 42 0
0 0 0 0 44
and
C =
0 0 41 4 0
45 12 0 0 15
6 43 0 0 10
0 0 23 42 0
26 27 0 0 44
How to obtain C from A and B. the example mentioned here is for (5,5) if it is for (50,100) how it can be done?
2 Comments
Birdman
on 2 Jan 2018
What is your condition for obtaining? There is not a certain pattern or something else for achieving C from A and B.
Accepted Answer
Rik
on 2 Jan 2018
Let me start off by saying you described it very poorly (and formatting your comment as code doesn't help).
What seems to be the goal is adding values to B. The rule for this is that in groups of rows, the entire column must be either 0 or contain a value. If B on that position is equal to 0, the value is taken from A.
For rows 1 and 4:
cluster=[0 0 41 0 0;
0 0 0 42 0];
non_0= sum(cluster);
non_0=repmat(non_0,2,1);
A_part=[1 2 3 4 5;
21 22 23 24 25];
new_cluster=cluster;
new_cluster(non_0 & cluster==0)=A_part(non_0 & cluster==0);
So now the question is how to generalize this to inputs of rows. Personally, I think you should be able to use this code a basis to write your own code for arbitrary row groups. The only thing you need to write is how to extract the clusters and put them back.
3 Comments
Rik
on 5 Jan 2018
What did you try? You are hopefully aware you can index matrices with a syntax like Y=X([1 3],:);?
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!