How to combine vectors based on value?

Good Afternoon,
Another quick question. Say I have a matrix,
B = [732145 732151 732168 0 0 0 0
284 281 297 0 0 0 0]
and
C =
[ 0 281 284 0 0 297 0
0 0 0 0 0 0 0]
I am trying to get it so that I have:
D = [0 281 284 0 0 297 0
0 732151 732145 0 0 732168 0].
Should I be using the [r c] kind of terminology. I keep almost getting there, I think, then I get confused.
Any thoughts would be greatly appreciated! or reading!
Thank you very much for any time!

답변 (3개)

Guillaume
Guillaume 2015년 10월 16일

1 개 추천

Regarding your latest comment, a much simpler way of obtaining your D result, assumming that elements of B and C are uniques is:
assert(numel(unique(C)) == numel(C) && numel(unique(B)) == numel(B)); %does not work if elements are not unique
[values, ic, ib] = intersect(C, B);
D = zeros(numel(C), numel(B));
D(sub2ind(size(D), ic, ib)) = values;
If the values of B and C are not unique, you need an additional level of indirection through unique.

댓글 수: 1

Chameleon17
Chameleon17 2015년 10월 16일
Thank you for that, i've not used the intersect before, that's good to know!
The values of B are all unique - but I may have repeats of the C values at certain times.

댓글을 달려면 로그인하십시오.

Andrei Bobrov
Andrei Bobrov 2015년 10월 16일

1 개 추천

B = [4 5 6 7; 0 0 0 0; 0 0 0 0; 0 0 0 0];
C = [0; 5; 4];
D = B;
[ll,idx] = ismember(B(1,:),C);
D(sub2ind(size(D),1+idx(ll),find(ll))) = C(idx(ll));
Geoff Hayes
Geoff Hayes 2015년 10월 14일

0 개 추천

Do you wish to simply iterate over each element in the first row of C and, for those that are non-zero, check to see if it exists in the second row of matrix B and copy the the value from its first row into D? If so, try the following
D = C;
for k=1:size(C,2)
if C(1,k) ~= 0
[idx] = find(B(2,:)==C(1,k));
if ~isempty(idx)
D(2,k) = B(1,idx);
end
end
end
In the above, we just iterate over each column in the first row of C. If the element is non-zero then we use find to find that element of the second row of B that matches the value in C. If the returned index, idx, is non-empty then we set this value to the corresponding element of D.

댓글 수: 5

Chameleon17
Chameleon17 2015년 10월 15일
Thank you very much! That's exactly what I need to do, and what I need to learn more about. Can I trouble you for another related problem?
Say I have:
B = [4 5 6 7; 0 0 0 0; 0 0 0 0; 0 0 0 0] C = [0; 5; 4]
and I'm trying to get to D which would be:
D = [4 5 6 7; 0 0 0 0; 0 5 0 0; 4 0 0 0]
I've been playing around with it for ages, but I'm still stumped.. I know it will be quite an easy solution...
Thank you again!
Geoff Hayes
Geoff Hayes 2015년 10월 15일
First you need to define the rules or steps on how you start with B and C and get to D. What are the steps? Once you know what they are, you should be able code it up. (I'm asking only because it isn't clear to me how D is created. Why is 5 in the second element of the third row of D? Why is 4 in the first element of the last row of D?)
Chameleon17
Chameleon17 2015년 10월 16일
편집: Guillaume 2015년 10월 16일
Thanks for the advice! It's a really simplified version of something I want to apply for a lot more data, I did it though!
Sometimes someone else commenting on it helps a lot, I really appreciate the advice on this forum and the time people contribute! You go round in circles in your own head sometimes with the simplest of things.
k = 1: length(B)
g = 1:length(C)
for h = 1:length(g)
for m = 1:length(k)
if C(h,1) == B(1,m)
D(h,m) = C(h,1)
end
end
end
Guillaume
Guillaume 2015년 10월 16일
편집: Guillaume 2015년 10월 16일
I assume you've made a typo and that the for loops are actually:
for h = 1:g
for m = 1:k
Chameleon17
Chameleon17 2015년 10월 16일
Ah yes, I had a bunch of them, it is also D(h+1,m) too!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2015년 10월 14일

댓글:

2015년 10월 16일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by