Azzera filtri
Azzera filtri

How to do the following with Matrix in MatLab

4 visualizzazioni (ultimi 30 giorni)
kk87
kk87 il 3 Gen 2013
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.

Risposte (1)

Rick Rosson
Rick Rosson il 4 Gen 2013
Modificato: Rick Rosson il 4 Gen 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 Commenti
Image Analyst
Image Analyst il 4 Gen 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
Rick Rosson il 4 Gen 2013
Modificato: Rick Rosson il 4 Gen 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.

Accedi per commentare.

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!

Translated by