Filter löschen
Filter löschen

how to combine 2 matrix at once with loop process..?

3 Ansichten (letzte 30 Tage)
Noru
Noru am 3 Apr. 2013
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 Kommentare
Matt Kindig
Matt Kindig am 3 Apr. 2013
Bearbeitet: Matt Kindig am 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).
Noru
Noru am 5 Apr. 2013
in my case above there's no problem with that, because line 4 and line 5 will applied in different matrix 'data'.
the problem is how to insert elements of matrix A and B per line to matrix 'data' so it will create 10 different matrix 'data' combination. one line for one matrix 'data'.
do you have any solutions..?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 5 Apr. 2013
Bearbeitet: Andrei Bobrov am 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 Kommentare
Andrei Bobrov
Andrei Bobrov am 7 Apr. 2013
see part ADD in my answer.
Noru
Noru am 16 Apr. 2013
ok thank you sir...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by