How to replace values in a matrix?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
aya ben mabrouk
am 6 Jun. 2016
Kommentiert: aya ben mabrouk
am 6 Jun. 2016
Hello everyone, I am new in matlab and I need help please.
I have a matrix A that contains probability distribution( size : 7*8).there is the matrix :
A=[ 0.2 0.07 0.25 0.87 0.09 0.14 0.8 0.3;
0.1 0.5 0.25 0.14 0.98 0.025 0.58 0.11;
0.001 0.25 0.88 0.21 0.03 0.25 0.58 0.25;
0.26 0.58 0.001 0.06 0.9 0.58 0.44 0.15;
0.12 0.58 0.16 0.08 0.15 0.47 0.33 0.15;
0.15 0.22 0.3 0.14 0.01 0.99 0.77 0.55;
0.003 0.25 0.14 0.99 0.005 0.85 0.26 0.003;
];
I have also a matrix B (size 7*4) that contains such A probabilities and matrix C (size 7*4) that contains positions.
B=[ 0.25 0.55 0.15 0.02;
0.001 0.25 0.33 0.04;
0.2 0.3 0.11 0.58;
0.25 0.14 0.77 0.69;
0.88 0.59 0.58 0.001;
0.02 0.3 0.7 0.8;
0.3 0.9 0.66 0.11;]
C= [ 8 2 4 1
1 2 3 4
7 5 2 8
1 2 3 4
2 4 3 7
1 5 6 7
2 8 4 1]
My goal is to replace values in A with values in the matrix B according to the position in C, I will have as result this matrix (size 7*8) :
NewA=[ 0.02 0.55 0.25 0.15 0.09 0.14 0.08 0.25
0.001 0.25 0.33 0.04 0.98 0.025 0.58 0.11
0.001 0.11 0.88 0.21 0.3 0.25 0.2 0.58
0.25 0.14 0.77 0.69 0.9 0.58 0.44 0.15
0.12 0.88 0.58 0.59 0.15 0.047 0.001 0.15
0.02 0.22 0.3 0.14 0.21 0.7 0.8 0.55
0.11 0.3 0.14 0.66 0.005 0.85 0.26 0.9]
Please, any idea, How Can I do it? and thanks in advance
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 6 Jun. 2016
Try a simple for loop:
A=[ 0.2 0.07 0.25 0.87 0.09 0.14 0.8 0.3;
0.1 0.5 0.25 0.14 0.98 0.025 0.58 0.11;
0.001 0.25 0.88 0.21 0.03 0.25 0.58 0.25;
0.26 0.58 0.001 0.06 0.9 0.58 0.44 0.15;
0.12 0.58 0.16 0.08 0.15 0.47 0.33 0.15;
0.15 0.22 0.3 0.14 0.01 0.99 0.77 0.55;
0.003 0.25 0.14 0.99 0.005 0.85 0.26 0.003;
];
B=[ 0.25 0.55 0.15 0.02;
0.001 0.25 0.33 0.04;
0.2 0.3 0.11 0.58;
0.25 0.14 0.77 0.69;
0.88 0.59 0.58 0.001;
0.02 0.3 0.7 0.8;
0.3 0.9 0.66 0.11;]
C= [ 8 2 4 1
1 2 3 4
7 5 2 8
1 2 3 4
2 4 3 7
1 5 6 7
2 8 4 1]
[rows, columns] = size(B)
newA = A; % Initialize.
for row = 1 : rows
newA(row, C(row,:)) = B(row, :);
end
newA % Print in command window to verify
Weitere Antworten (0)
Siehe auch
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!