random assignment to 1s in a matrix

1 回表示 (過去 30 日間)
Berfin Çetinkaya
Berfin Çetinkaya 2022 年 3 月 16 日
コメント済み: Berfin Çetinkaya 2022 年 3 月 16 日
c= birincifaz(:,:)
x=find(c==1)
ja=randi(613,1)
c(x(ja))= randi(10,1)
Hi. I have set up such code. My matrix is very large in 10x69 structure, I want to assign a random number from one to eighteen where 1 is written in the matrix. and it takes too long to assign manually so I want to assign by code. I was able to assign 1 to 1 with this code. How can I set up code about assigning to other 1's? ( Caution ! I wrote birincifaz values below.)
thanks a lot of
birincifaz=
1 1 1 1 1 0 1 1 1 1
0 0 0 0 0 0 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 0 1 1 0 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1
0 0 0 0 0 0 1 0 0 0
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0 0
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 0 0 0
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1

採用された回答

Jan
Jan 2022 年 3 月 16 日
編集済み: Jan 2022 年 3 月 16 日
I do not understand the question:
"I was able to assign 1 to 1 with this code." - what is the purpose of setting a 1 to a 1?
"How can I set up code about assigning to other 1's?" - which other 1's? Ah, you have changed one single 1 and want to change all. Then:
c = birincifaz; % No need for: (:,:), no need for FIND.
c(c == 1) = randi(18, 1, nnz(c))
  1 件のコメント
Berfin Çetinkaya
Berfin Çetinkaya 2022 年 3 月 16 日
This is the correct answer! Thank you so much.

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

その他の回答 (1 件)

Davide Masiello
Davide Masiello 2022 年 3 月 16 日
編集済み: Davide Masiello 2022 年 3 月 16 日
c = randi([0,1],69,10) % This replaces your biricinfaz, didn't want to copy that ;)
c = 69×10
0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 1 1 0 0 1 1 1 1 0 1 0 0 1 0 1 1 0 1 1 1 1 1 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 0 1 0 1 1 1 0 1 1 1 0 1 0 1 0 1 0 0 1 1 0 0 0 1 0 1 1 1 0 1 1 1 0 1 0 0 0 1 1 0 0 0
c(logical(c)) = randi(18,sum(c(:)),1) % This insert random values up to 18 in place of the 1s in c
c = 69×10
0 0 0 0 13 6 14 0 0 0 0 0 0 0 8 11 13 0 7 14 0 0 13 3 10 17 0 11 0 0 8 0 7 2 0 2 1 7 7 16 0 0 8 0 0 0 14 0 12 0 16 0 0 0 10 0 6 0 0 3 0 2 7 8 0 9 5 6 0 6 0 16 0 13 0 0 8 12 0 0 0 14 0 18 11 3 0 11 9 3 0 10 0 0 0 10 6 0 0 0
  1 件のコメント
Berfin Çetinkaya
Berfin Çetinkaya 2022 年 3 月 16 日
Your code worked. This is the correct answer. Thank you for your help.

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

カテゴリ

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