Effacer les filtres
Effacer les filtres

Replacing values for matrices of different dimension

4 vues (au cours des 30 derniers jours)
RDG
RDG le 12 Sep 2013
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 commentaires
Walter Roberson
Walter Roberson le 12 Sep 2013
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 le 12 Sep 2013
Sorry for the late reply. Yes, you're correct.

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 12 Sep 2013
Modifié(e) : Andrei Bobrov le 12 Sep 2013
[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

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by