For loop over writes output with strcmp

Hello,
I have the following code. The probles is that for every for loop, there are mulitple occurences of firsy in second. So, after every run, L is over written. I would like the indexesfor of anything that matches to be added below the previous output of L, not overwritten. I tried a lot of things but couldn't figure this out. Please help.
for a=1:r
first=test1(a,1);
second=reactionType(:,3);
L=find(strcmp(second,first));
end

2 comentarios

Would be real nice if you told what
test1
reationType
is...
Ishita Trivedi
Ishita Trivedi el 31 de Dic. de 2018
they are both cell arrays of varying columns and rows containing mixed data (numbers and strings). test1 is basically my input array that where the 1st column needs to be cross-checked against the 3rd column of reactionType array for any matches. Then I need the index of those matches.

Iniciar sesión para comentar.

 Respuesta aceptada

madhan ravi
madhan ravi el 31 de Dic. de 2018
Editada: madhan ravi el 31 de Dic. de 2018
L=cell(1,numel(r)); % before loop (preallocation)
L{a}.... inside loop
^^^---just add this rest the same
celldisp(L) % after loop (to see the contents of L)

9 comentarios

Ishita Trivedi
Ishita Trivedi el 31 de Dic. de 2018
Okay that worked. However, now L is a cell array (1,r) where:
If there is data, the cell is a 11x1 double (for example) and if there is no data, i see [ ] (meaning blank or no matches were found).
Is there a way to trim L to only save cells where there is data and ignore the rest?
madhan ravi
madhan ravi el 31 de Dic. de 2018
Editada: madhan ravi el 31 de Dic. de 2018
idx=~cellfun('isempty',L);
L(idx) % this will avoid all the empty arrays
Ishita Trivedi
Ishita Trivedi el 31 de Dic. de 2018
I am sorry for my dumb questions but I am just learning matlab as I work on this. So I don't quite understand where to add this line?
madhan ravi
madhan ravi el 31 de Dic. de 2018
Don't be sorry , at the end of the code.
Ishita Trivedi
Ishita Trivedi el 31 de Dic. de 2018
This worked perfectly. Thank you so much.The three things I am struggling with in matlab is learning how to use textscan, format (when using text scan) and cellfunc properly.
I am sure it will be easier once I get better at this. But thank you for helping.
madhan ravi
madhan ravi el 31 de Dic. de 2018
Anytime :) , read the documentation of textscan() it has plenty of examples.
Ishita Trivedi
Ishita Trivedi el 31 de Dic. de 2018
Thank you!
I actually have a follow up. When I store the index of all matches in L{a} - in column 2 of L{1,1} (for example),how can I also store the input value from test_1 that is being compared to the master library reactionType inside for loop?
index comes from master library and input from test_1.
madhan ravi
madhan ravi el 31 de Dic. de 2018
just preallocate as shown in the answer and follow it
Ishita Trivedi
Ishita Trivedi el 31 de Dic. de 2018
Hello,
The problem I am encountering here is how to have string at L{1,1}(1,2) while L{1,}(1,1) is a number?
I keep getting NaN.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Productos

Versión

R2018b

Etiquetas

Preguntada:

el 31 de Dic. de 2018

Comentada:

el 31 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by