フィルターのクリア

change only some cell values to a value of 1

11 ビュー (過去 30 日間)
Conner Carriere
Conner Carriere 2022 年 11 月 12 日
回答済み: Voss 2022 年 11 月 12 日
I want to change all the zeros in my cell to a value of 1. I know this could be done with a for and if statement, but is there anything that is faster?
Here is my data
[0,241,638,168]
[204,181,382,287;56,0,185,244]
[0,59,574,506]
[8,58,231,546]
[28,68,375,247]
[115,82,524,510]
Want it to look like this
[1,241,638,168]
[204,181,382,287;56,1,185,244]
[1,59,574,506]
[8,58,231,546]
[28,68,375,247]
[115,82,524,510]
Would it be some sort of cellfun() function?
  2 件のコメント
Conner Carriere
Conner Carriere 2022 年 11 月 12 日
A(A == 0) = 1 works for arrays, but not for cells.
Conner Carriere
Conner Carriere 2022 年 11 月 12 日
{cat(1, a{1,1:4})} works alright once i convert it to a matrix, but then I would have to do a big for loop afterwards for each row. In the end I need a 4x1 cell with [x x x x]

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

採用された回答

Voss
Voss 2022 年 11 月 12 日
Yes, you can use cellfun with a helper function:
C = {
[0,241,638,168]
[204,181,382,287;56,0,185,244]
[0,59,574,506]
[8,58,231,546]
[28,68,375,247]
[115,82,524,510]
};
C_new = cellfun(@zeros2ones,C,'UniformOutput',false)
C_new = 6×1 cell array
{[ 1 241 638 168]} {2×4 double } {[ 1 59 574 506]} {[ 8 58 231 546]} {[ 28 68 375 247]} {[115 82 524 510]}
function A = zeros2ones(A)
A(A == 0) = 1;
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by