Borrar filtros
Borrar filtros

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

12 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Voss
Voss el 15 de Feb. de 2023
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 comentarios
Voss
Voss el 16 de Feb. de 2023
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 el 16 de Feb. de 2023
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.)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by