How to simplify this and make it efficient

I have a 1x500 cell (res) with a structure in each, and a 300x3 table (G). I need to check for each combination if strcmp between the field res{j}.fir and each row in G.
If it is equal I need to add the field from res 'thisisstring' to a new 4. column in the table G. The following works but is so slooow....
for i = 1:size(G{:,1},1)
for j = 1:size(res,2)
if strcmp(res{j}.fir,G{i,1}{1})
G{i,4} = res{j}.thisisstring;
end
end
end
If someone have an idea to simplify this it would be great!

 Respuesta aceptada

Bruno Luong
Bruno Luong el 5 de Sept. de 2019
Editada: Bruno Luong el 6 de Sept. de 2019
NOTE: what a messy and bad data structure. There might be a much better way than working cell of structs and table.
% Generate test data
S = string(ceil(10*rand(300,3)));
G = table(S(:,1),S(:,2),S(:,3));
fir = num2cell(string(ceil(10*rand(1,500)+5)));
thisisstring = num2cell(string(ceil(100*rand(1,500)+100)));
res = num2cell(struct('fir',fir,'thisisstring',thisisstring));
clear S fir thisisstring % only G and res stay
% Engine here, do the same thing than your double for loops
[b,loc] = ismember( G{:,1}, cellfun(@(r) r.fir, res));
G(b,4) = table(cellfun(@(r) r.thisisstring, res(loc(b)))');
% Check the result
G

7 comentarios

Nice one Bruno! Genious!
I had to alter a little bit to make it work (to fit my variables):
[b,loc] = ismember( G{:,1}, cellfun(@(r) string(r.fir), res));
G(b,4) = table(cellfun(@(r) string(r.thisisstring), res(loc(b)))');
Since I apparently use char and not string as I said. Works efficiently.
Thanks a alot!
- Best
Bruno Luong
Bruno Luong el 6 de Sept. de 2019
"Since I apparently use char and not string as I said"
I must miss where you said it's char or string but it takes be a big guess to (almost) figure out what your data contain.
Next time it is better if you could describe more and/or attach some data.
Martin
Martin el 6 de Sept. de 2019
I think I gave enough info and you also did, I don't expect people to fix my problem completely without even working on it. Then I never learn anything. Anyway it was almost a complete solution anyway, don't think I'm unhappy:)
Bruno Luong
Bruno Luong el 6 de Sept. de 2019
Editada: Bruno Luong el 6 de Sept. de 2019
It's not only question of you are unhappy or expect someone to completely solve your problem.
Let me explain: You see 5 lines of my code after % Generate test data.
It took me more than 15 minutes to do it, until your for-loop finally runs. If you provide the data for convenience then it would save me some time and I was also happy.
Martin
Martin el 6 de Sept. de 2019
Well, with such a question I gave above, I only hope for a direction from people, - that is why I asked if someone have an "idea". Your answer was in details and that is nice, but no one forced you to make that work and answer me. I replied I had to alter a thing just in case if others search for a corresponding problem and got difficulties with your functions (like I had).
Bruno Luong
Bruno Luong el 6 de Sept. de 2019
Editada: Bruno Luong el 6 de Sept. de 2019
I'm not forced, but I don't like to lost my time for something that I can avoid, because I don't like to communicate back/forth with people who asks, gets a solution and discover that it doesn't exactly solve their problem because they failed to give complete details then they are stuck.
This is fortunately not your case, but most of the people who asks are like this.
This is what we (helpers) resquest is very well stated in this tutorial.
You should read especially point No. 5
"A common cause for delays in getting an acceptable solution is posting code that doesn't run because it was snipped out of the middle of a program and required variables were assigned in code above that was not included with the Answers post."
Martin
Martin el 6 de Sept. de 2019
Editada: Martin el 6 de Sept. de 2019
I know that is frustrating and that makes completly sense! I would never get back to you with a question like mine. That's why I spent 4-5 hours after your answer to get the string() solution. I knew if someone had a direction of an algorithm I could figure it out. After I did that, I accepted your answer, gave a like, and added a little comment that probably can help others. Thanks Bruno and especially for your answer!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos

Preguntada:

el 5 de Sept. de 2019

Editada:

el 6 de Sept. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by