Replacing values for matrices of different dimension

2 ビュー (過去 30 日間)
RDG
RDG 2013 年 9 月 12 日
Suppose,
A= [15 1 4 2 65
15 2 4 6 65
17 6 5 2 65
24 5 3 1 55
24 5 5 3 55
25 2 1 1 55
30 2 1 1 20
31 5 2 2 11
31 5 3 5 11
33 3 2 2 31
33 4 5 3 31
57 3 2 4 2
58 4 5 5 2];
B= [1 1 1
1 1 5
1 1 6
1 2 3
1 3 2
2 1 4
2 1 6
2 2 2
2 2 5
2 3 2
3 1 6
3 2 1
3 2 6
3 3 3
3 3 4
3 3 5
3 4 2
3 4 3
3 4 4
3 4 5
4 1 1
4 2 1
4 2 3
4 2 4
4 2 5
4 3 6
4 6 6
5 1 1
5 1 2
5 1 3
5 1 4
5 1 5
5 4 3
5 5 1
5 6 6
6 1 2
6 1 4
6 1 6
6 2 1
6 2 2
6 2 3];
Both A and B are matrices of different dimensions. I would like to substitute the values in A(:,2:4) with values from B(:,1:3) based on A(column 2).
Resultant (Something like this):
Resultant=[ 15 1 1 1 65
15 2 1 4 65
17 6 1 2 65
24 5 1 1 55
24 5 5 3 55
25 5 1 2 55
30 2 1 6 20
31 5 1 3 11
31 5 1 4 11
33 3 1 6 31
33 4 1 1 31
57 3 2 1 2
58 4 2 1 2];
  2 件のコメント
Walter Roberson
Walter Roberson 2013 年 9 月 12 日
What is the rule? That when you encounter N in the second column of A, that you do a replacement in columns 2:4 with the first "unused" line in B that has the same value N in the first column? So the first 6 in A(:,2) is matched with the first 6 in B(:,1), the second 6 in A(:,2) is matched with the second 6 in B(:,1) and so on ?
RDG
RDG 2013 年 9 月 12 日
Sorry for the late reply. Yes, you're correct.

サインインしてコメントする。

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 9 月 12 日
編集済み: Andrei Bobrov 2013 年 9 月 12 日
[a,ia0] = sort(A(:,2));
ba = histc(a,unique(a));
t1 = [true;diff(B(:,1))~=0] + 0;
t1(find(t1) + ba)= -1;
t3 = cumsum(t1);
[ii,ii] = sort(ia0);
b = B(t3>0,:);
out = A;
out(:,2:4) = b(ii,:);
or
[ba,i00] = histc(A(:,2),unique(A(:,2)));
out = A;
for jj = 1:numel(ba)
t = find(B(:,1) == jj);
out(i00 == jj,2:4) = B(t(1:ba(jj)),:);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by