フィルターのクリア

Is there a command that can check if a string is present in a certain array and output that same string?

12 ビュー (過去 30 日間)
Hi i want to make checks to some string array with variable dimension and i tried doing what's written in the code. I know the problem is the code in the "if" but i don't know what to do... All the functions i know that find a string in an array return an array of logical 0 and 1. I can transform the array in the same dimension by putting 0 in all the seasons that aren't present and add some || in the if statement but i feel it's less practical, and i simply want to know if it's possible.
%The first column is for other purposes
L = {1, ["Spring", "Summer"]; 0, ["Winter", "Summer","Autumn"]}
i=0;
while (i<2)
i = i+1;
if (L{i,2}(:)=="Winter")
fprintf("Yes")
end
end

採用された回答

Voss
Voss 2023 年 2 月 15 日
I'm confused about what you want the output to be.
L = {1, ["Spring", "Summer"]; 0, ["Winter", "Summer","Autumn"]};
ism = cellfun(@(x)ismember("Winter",x),L(:,2))
ism = 2×1 logical array
0 1
  7 件のコメント
Voss
Voss 2023 年 2 月 16 日
Exactly. It's the same as this:
C = {[1 2 3],[4 5 6 7],[8 9]};
f = @(x)x(1);
cellfun(f,C) % apply the function f to the contents of each cell of C
ans = 1×3
1 4 8
Walter Roberson
Walter Roberson 2023 年 2 月 16 日
ism = cellfun(@(x)ismember("Winter",x),L(:,2))
is the same as
InternalTemporaryVariable = @(x)ismember("Winter",x);
ism = cellfun(InternalTemporaryVariable, L(:,2));
clear InternalTemporaryVariable
That is, the temporary anonymous function is created before cellfun is called, and is removed after the call (because there are no more references to it.)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by