フィルターのクリア

How can I replace matrix with random generated number (int) and position?

1 回表示 (過去 30 日間)
Noru
Noru 2013 年 1 月 21 日
Hello,
I'am trying to combine 2 matrix, I have 1 matrix with fix number, column, and rows. The other one consist of 2 column matrix with random rows (this matrix more little than matrix 1) and I have to generate it first, randomly. For the example is :
matrix 1 : X Y Z
1 0.0 2
2 0.0 5
3 0.0 1
4 0.0 3
5 0.0 0
matrix 2 : A B
1 0
2 8
4 6
Based on the example, column A used to replace column X and column B used to replace column Z. The value of column A show us where to replace the value of column B in Z.
  • When the value of A is 2, then the value of Z in row 2 which is '5' replace by '8' as the value of B.
  • When the value of A is 4, then the value of Z in row 4 which is '3' replace by '6' as the value of B.
  • But there is an exception, the value of column Z row 1 should not be changed, so when the generated value of A is 1, the value of Z in row 1 which is '2' should not replace by the value of column B and still worth '2'.
  • This program will loop with 100 of maximum iterations, and in every iteration, matrix 2 will generated randomly.
Does anyone have the solution..?
Thanks.
  2 件のコメント
Walter Roberson
Walter Roberson 2013 年 1 月 21 日
In Matrix 2, can there be multiple rows with the same A but different B?
Noru
Noru 2013 年 1 月 28 日
I don't really understand with function of multiple rows with the same A but different B.. But if you think have a solution, it will very welcome and thank you.. :)

サインインしてコメントする。

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 1 月 21 日
eg
A = [(1:20)', zeros(20,1), randi([0 20],20,1)];
for jj = 1:100
n = rand(20,1) > rand(); %
B0 = randi([0 10],nnz(n),1); % random generation of the matrix 2 (B)
B = [A(n,1), B0]; %
t = B(:,1) > 1;
A(ismember(A(:,1),B(t,1)),3) = B(t,2);
end
  1 件のコメント
Noru
Noru 2013 年 1 月 28 日
thanks..
it really works..
thank you...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by