Make a new variable based on data in the cell array.

6 ビュー (過去 30 日間)
Saim Ehtesham
Saim Ehtesham 2023 年 6 月 13 日
コメント済み: Saim Ehtesham 2023 年 6 月 13 日
I have a cell array called C. Each row in C has a different set of integerS stored as a cell
I wan to write a code such that it looks in each row/cell of C, if any integer in the row is equal to 1, it stores in the new variable x, the cell number.
I have the code:
x = [];
for i = 1:numel(C)
if any(C{i}==1)
x(i) = i; %store index of that cell %i know this line is the problem
end
end
problem is this creates length of x same as length of C with zeros in between. I dont want those zeros, instead just a vector of indices where a cell of C has the value 1.
for example right now, the out put is :
1 2 3 0 0 6
where as I want something like this:
1 2 3 6

採用された回答

Matt J
Matt J 2023 年 6 月 13 日
編集済み: Matt J 2023 年 6 月 13 日
x=[1 2 3 0 0 6]
x = 1×6
1 2 3 0 0 6
x=nonzeros(x)'
x = 1×4
1 2 3 6
  1 件のコメント
Saim Ehtesham
Saim Ehtesham 2023 年 6 月 13 日
Thanks for all your answers

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

その他の回答 (2 件)

Matt J
Matt J 2023 年 6 月 13 日
編集済み: Matt J 2023 年 6 月 13 日
x=[1 2 3 0 0 6]
x = 1×6
1 2 3 0 0 6
x=x(x~=0)
x = 1×4
1 2 3 6

Matt J
Matt J 2023 年 6 月 13 日
x=[1 2 3 0 0 6]
x = 1×6
1 2 3 0 0 6
x=x(logical(x))
x = 1×4
1 2 3 6

カテゴリ

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

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by