フィルターのクリア

How to put the lengths of each string in a cell array of strings into a single vector?

16 ビュー (過去 30 日間)
I have tried in vain to put this one together. This does work: s = seq(n,:),where n is the specific line for a the string. I've tried using the length and size functions, but I can't get the individual row lengths into a vector. I've also searched through the function lists and looked for similar questions. Ideally, I want to index the cell's in their original order, and put the index number next to the length value. I know how to create a vector with numbers 1 to 100 --v = 1:1:100, but I'm not sure how to line that up and add it to the string array. I apologize for my newbiness, I admit this is a beginner question. Any feedback is appreciated. thx

採用された回答

Ken Atwell
Ken Atwell 2011 年 6 月 6 日
If I understand your question correctly:
A = {'Hello', 'World!!!'}
B = cellfun(@length, A)
cellfun executes a function over every element in a cell array.
  1 件のコメント
Adam Quintero
Adam Quintero 2011 年 6 月 7 日
This works quite well. Thank you for the explanation of cellfun, it is quite helpful. It gets rid of the overhead of a loop, and I can already see tons of applications.
Much thanx

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

その他の回答 (2 件)

Rob Graessle
Rob Graessle 2011 年 6 月 6 日
Not a particularly elegant solution, but it works:
>> a={'the', 'quick', 'brown', 'fox'}
a =
'the' 'quick' 'brown' 'fox'
>> for ii=1:length(a), wordLengths(ii)=length(a{ii}); end
>> wordLengths
wordLengths =
3 5 5 3

Matt Fig
Matt Fig 2011 年 6 月 6 日
a={'the', 'quick', 'brown', 'fox'}
L = cellfun('size',a,2)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by