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 件のコメント

Michael Madelaire
Michael Madelaire 2018 年 12 月 31 日
Would be real nice if you told what
test1
reationType
is...
Ishita Trivedi
Ishita Trivedi 2018 年 12 月 31 日
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.

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

 採用された回答

madhan ravi
madhan ravi 2018 年 12 月 31 日
編集済み: madhan ravi 2018 年 12 月 31 日

0 投票

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 件のコメント

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

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

製品

リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by