Is it possible to reduce this for loop into one line of code?

6 ビュー (過去 30 日間)
Leon
Leon 2025 年 3 月 12 日
コメント済み: Matt J 2025 年 3 月 13 日
My goal is to simplify the for-loop below to a one line of code. Please see attached for a mat file storing the Headers variable. Many thanks for any recommendations
% Find missing values in the Headers cell array:
Ind1 = cellfun(@(X)any(ismissing(X),'all'), Headers);
for i=1:numel(Ind1)
if Ind1(i)
Headers{i} = ['Missing_header_', num2str(i)];
end
end

採用された回答

Voss
Voss 2025 年 3 月 12 日
Ind1 = cellfun(@(X)any(ismissing(X),'all'), Headers);
Headers(Ind1) = cellstr("Missing_header_"+find(Ind1));
  2 件のコメント
Leon
Leon 2025 年 3 月 12 日
Amazing. Thank you so much.
Voss
Voss 2025 年 3 月 12 日
You're welcome!

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

その他の回答 (3 件)

Walter Roberson
Walter Roberson 2025 年 3 月 12 日
Headers(cellfun(@(X)any(ismissing(X),'all'), Headers)) = cellstr("Missing_header_" + find(cellfun(@(X)any(ismissing(X),'all'), Headers)));
Just because you can do it on one line doesn't mean that you should do it on one line. I would recommend at least two lines
mask = cellfun(@(X)any(ismissing(X),'all'), Headers);
Headers(mask) = cellstr("Missing_header_" + find(mask));
  1 件のコメント
Leon
Leon 2025 年 3 月 12 日
Many thanks for the solution!

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


Matt J
Matt J 2025 年 3 月 13 日
Headers = findMissing(Headers); %One line
  2 件のコメント
dpb
dpb 2025 年 3 月 13 日
<chuckles...>
Matt J
Matt J 2025 年 3 月 13 日
Thanks, but I'm serious! All too often I see OP's forgetting that the whole point of user-defined functions is to reduce the number of lines of code.

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


dpb
dpb 2025 年 3 月 12 日
編集済み: dpb 2025 年 3 月 12 日
load test11
whos
Name Size Bytes Class Attributes Headers 1x6 826 cell ans 1x34 68 char
Headers
Headers = 1x6 cell array
{'Cruise ID'} {'Flag'} {'Missing_header_3'} {'Temp'} {'Missing_header_5'} {'EXPO'}
There aren't any missing in Headers so we'll make up some...
H=Headers;
H(3)={''}; H(5)=H(3)
H = 1x6 cell array
{'Cruise ID'} {'Flag'} {0x0 char} {'Temp'} {0x0 char} {'EXPO'}
H(ismissing(H))=compose('Missing_header_%d',find(ismissing(H)))
H = 1x6 cell array
{'Cruise ID'} {'Flag'} {'Missing_header_3'} {'Temp'} {'Missing_header_5'} {'EXPO'}
is one line of code albeit in order to do that one has to call ismissing twice't instead of using a temporary as did @Walter Roberson with whom I don't disagree that sometime just because can isn't necessarily any better than straightforward. Although I'd not use a loop here, I would probably choose the temporary since to satisfy the request the find is needed to get the positions...
ix=find(ismissing(H));
H(ix)=compose('Missing_header_%d',ix)
H = 1x6 cell array
{'Cruise ID'} {'Flag'} {'Missing_header_3'} {'Temp'} {'Missing_header_5'} {'EXPO'}
using compose to build an array of cellstrings instead of the new(ish) string class wherein numbers are silently converted to string representation and the plus operator is overloaded to concatenate strings that @Voss used.
  1 件のコメント
Leon
Leon 2025 年 3 月 12 日
Many thanks for the solution.

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

カテゴリ

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

タグ

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by