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 commentaires

Would be real nice if you told what
test1
reationType
is...
Ishita Trivedi
Ishita Trivedi le 31 Déc 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.

Connectez-vous pour commenter.

 Réponse acceptée

madhan ravi
madhan ravi le 31 Déc 2018
Modifié(e) : madhan ravi le 31 Déc 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 commentaires

Ishita Trivedi
Ishita Trivedi le 31 Déc 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 le 31 Déc 2018
Modifié(e) : madhan ravi le 31 Déc 2018
idx=~cellfun('isempty',L);
L(idx) % this will avoid all the empty arrays
Ishita Trivedi
Ishita Trivedi le 31 Déc 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 le 31 Déc 2018
Don't be sorry , at the end of the code.
Ishita Trivedi
Ishita Trivedi le 31 Déc 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 le 31 Déc 2018
Anytime :) , read the documentation of textscan() it has plenty of examples.
Ishita Trivedi
Ishita Trivedi le 31 Déc 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 le 31 Déc 2018
just preallocate as shown in the answer and follow it
Ishita Trivedi
Ishita Trivedi le 31 Déc 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Produits

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by