How to assign numbers to random cells.
5 views (last 30 days)
Show older comments
let n=5 m=zeros(n,n) m=rand(n,n)<0.1
and suppose the result of the above code is
m = [0 0 0 0 0; 0 0 1 0 0; 0 0 0 0 1; 0 0 0 1 0; 0 0 0 0 0]
consider the ones as the nucleation site. Now I want to assign different numbers in exact positions (in the random position of ones) as grain numbers i.e assign two values in the same cell. How to proceed.
0 Comments
Answers (1)
Guillaume
on 13 Jul 2015
Unless you change your matrix into a cell array, you can't assign two numbers to the same position, so I'm unclear what you're asking.
To assign numbers to only the non-zeros elements of m:
m(logical(m)) = rand(1, sum(m(:)))
3 Comments
Guillaume
on 13 Jul 2015
m is not a cell array. Matrices and cell arrays are two very different things. A matrix can only hold a single number in a cell, a cell array can have anything (including a matrix) in a single cell.
As per my answer, to assign numbers to the non-zeros elements of m:
m(logical(m)) = [5 10 ....] %there must be as many numbers to assign as there are 1 in m
What is not clear is how you decide which numbers to assign and more importantly how many in total.
See Also
Categories
Find more on Particle & Nuclear Physics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!