How to make specific elements of a cell equal to zero?

4 次查看(过去 30 天)
I have a cell 41 * 41 dimension and I would like to make the values that over 0.5 zeros and the rest of the values keep them without any changes.
Any assistance, please?

采纳的回答

Chris
Chris 2022-9-15
编辑:Chris 2022-9-15
If there's a single value in each cell, you could first convert to a matrix.
A = num2cell(rand(3))
A = 3×3 cell array
{[0.2044]} {[0.1296]} {[0.9499]} {[0.3952]} {[0.7477]} {[0.1062]} {[0.9799]} {[0.7201]} {[0.6477]}
Amat = cell2mat(A);
Amat(Amat>.5) = 0;
A = num2cell(Amat)
A = 3×3 cell array
{[0.2044]} {[0.1296]} {[ 0]} {[0.3952]} {[ 0]} {[0.1062]} {[ 0]} {[ 0]} {[ 0]}

更多回答(1 个)

Walter Roberson
Walter Roberson 2022-9-15
newCell = cellfun(@(M) M.*(M<=0.5), YourCell, 'uniform', 0)
However this will fail in locations that contain infinite values.

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by