how to combine 2 matrix at once with loop process..?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have a hard case (in my opinion), and i need your favor for this thing..
I have a data like below :
data = [1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0];
And 2 matrix like below :
A =
11 3
17 1
5 19
6 18
6 16
18 7
11 15
14 13
4 2
20 4
B =
4.0078 1.9147
4.9880 1.8967
1.7225 1.1802
1.1200 3.1427
2.6425 4.9374
2.4181 4.8850
3.6921 2.9100
3.0406 4.6255
3.5859 4.3326
2.4275 3.6506
I want to change the value in matrix 'data' row 2 with matrix B, the changes according to the matrix A. Matrix A use as a number that will synchronize with matrix 'data' row 1. For example, let see the first line of matrix A and matrix B. In matrix 'data' row 2 the value in line 11 and 3 (elements of matrix A) will change from 0 to 4.0078 and 1.9147 (elements of matrix B), so the matrix data will change like below :
data = [1 0
2 0
3 1.9147
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 4.0078
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0 ];
The changes will looping per line of matrix A and B, so the results will generates 10 different matrix data combination. I already tried to coding it, but there’s still wrong and I stuck with it. My coding shown below :
A = zeros(10,2);
B = zeros(10,2);
for C = 1:10,
d = 20;
a = randperm(d);
a = a(1:2);
A(C,:) = a;
b = (1+(5-1).*rand(1,2));
B(C,:) = b;
end
for C = 1:10
for i1 = 1:size(a),
for i2 = 1:size(b),
index = a(i1);
if data(index, 2) == 0
data(index, 2) = b(i2);
end
end
end
end
Did anyone had the solution..?
Thank you..
2 Commenti
Matt Kindig
il 3 Apr 2013
Modificato: Matt Kindig
il 3 Apr 2013
How do you handle the case where A has repeated indices, but B does not have repeated values? For example, in your shown data, the A(4,1) and A(5,1) elements both =4, but B(4,1) ~= B(5,1).
Risposta accettata
Andrei Bobrov
il 5 Apr 2013
Modificato: Andrei Bobrov
il 7 Apr 2013
sa = size(A,1);
out = accumarray([A(:), repmat((1:sa)',2,1)],B(:),[size(data,1) sa]);
or
sa = size(A,1);
sd = size(data,1);
out = zeros(sd,sa);
out(sub2ind([sd,sa],A,repmat(1:sa,2,1)')) = B;
ADD
sd = size(data,1);
data = reshape(num2cell(permute(cat(3,repmat((1:sd)',1,sa),out),...
[1 3 2]),[1 2]),[],1);
3 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!