How to do the following with Matrix in MatLab
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi! to all...am new to this site and this is my first thread... Warm regards..
I want to achieve the following:
First I want to create this matrix:
1 2 3
1 0 0 0
2 0 0 0
3 0 0 0
Then I want to create a Base Matrix(A matrix with predefined fixed values):
1 2 3
1 0 1 0
2 2 0 0
3 0 0 0
Then I want Random matrix from the Base matrix by adding +1 to +4 . but my constraint is that none of the number should be more than 4 so if the value is 0 then +4 is accepted but if the value is 2 then the accepted values should be +1 or +2..
Example of a randomly generated matrix from the base matrix will be:
1 2 3
1 0 1 1
2 4 0 1
3 0 2 0
now the twist here is that i also want to randomize the addresses along with the randomization of the values,
have a look
from this
1 2 3
1
2
3
to like this
3 2 1
3
2
1
and this
1 3 2
1
3
2
and so on...
now the last and my main program is to interchange the addresses of two matrix by keeping their values constant.
For example consider the following two matrix:
Matrix A:
3 2 1
3 0 1 1
2 4 0 1
1 0 2 0
Matrix B:
1 2 3
1 0 1 2
2 1 0 3
3 1 4 0
now the interchange of addresses will be:
Matrix B:
3 2 1
3 0 1 2
2 1 0 3
1 1 4 0
Matrix A:
1 2 3
1 0 1 1
2 4 0 1
3 0 2 0
Notice that the Values of Matrix B & A are constant, only the Addresses are interchanged..
so I want these. Kind help me to achieve it in MatLab... Thanks in advance.
1 Kommentar
Antworten (1)
Rick Rosson
am 4 Jan. 2013
Bearbeitet: Rick Rosson
am 4 Jan. 2013
Here is a start:
A = zeros(3,3);
B = zeros(3,3);
B(2,1) = 2;
B(1,2) = 1;
[M,N] = size(B);
Q = 4
C = B;
for k = 0:Q-1
imax = Q - k;
C = C + (B == k) .* randi(imax,M,N);
end
For more information:
>> doc randi
>> doc zeros
>> doc size
2 Kommentare
Image Analyst
am 4 Jan. 2013
Don't you have to add
C = min(C, 4);
for the constraint that no number, after addition of the random numbers, exceeds 4?
Rick Rosson
am 4 Jan. 2013
Bearbeitet: Rick Rosson
am 4 Jan. 2013
Although he didn't specify, my assumption is that he wants equiprobable outcomes. If you simply take the min, the result will be biased too heavily to a value of 4. I modified my solution to account correctly for this constraint.
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!