Finding an element of a cell in a cell array

2 次查看(过去 30 天)
If I have a cell array:
C= [ [1 2 3], [2 2 3], [5 2 7 10 2 1], [12 10 22 0] ,[4 7 5] ]
I want to locate all of the 1's ( so C{1}(1), and C{3}(6) ), and replace them with two random numbers. How might I go about doing that?

采纳的回答

Cedric
Cedric 2014-7-1
编辑:Cedric 2014-7-1
A simple loop would do
C = { [1 2 3], [2 2 3], [5 2 7 10 2 1], [12 10 22 0] ,[4 7 5] } ;
for cId = 1 : numel( C )
is1 = C{cId} == 1 ;
if any( is1 )
C{cId}(is1) = rand( 1, nnz(is1) ) ;
end
end
outputs C with..
>> C{1}
ans =
0.6324 2.0000 3.0000
>> C{2}
ans =
2 2 3
>> C{3}
ans =
5.0000 2.0000 7.0000 10.0000 2.0000 0.0975
If you need a random integer -> doc randi

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by