How to run completely a loop inside one other?

Hi! i have the data attached. ID_stop1 is an identifier of data and it is repeated 80 times for each value. Nn1 is a number from 1 to 20 and it is still repeated for each unique value of ID_stop1. Bn1 is a number from 1 to 20 and it is repeated for each unique value of Nn1. I need to access to data of L1 for each ID_stop1 and each Nn1. I created this code that works but at the end of the second loop it stops. How can i come back to the first loop?
I would like to have a cell array of 20 cells, and for each cell there should be 20 sub cells.
Any helps?
load ('sclerometrica_equotip_v1')
misure = [ID_stop L Nn Bn];
% misure(any(isnan(misure), 2), :) = [];
ID_stop1 = misure(:,1);
L1 = misure(:,2);
Nn1 = misure(:,3);
Bn1 = misure(:,4);
k = unique(ID_stop1);
for i = 1:numel(k)
index = (ID_stop1 == k(i));
N= Nn1(index); %battute nodi
j = unique(N);
for m = 1:numel(j)
index2 = (N == j(m));
L_nodo = L1(index2);
result{m} = L_nodo;
end
end

 Accepted Answer

I suspect that the issue is not that the code actually stops, but that the results are only recorded for one internal loop. If you want cells inside of cells then you need to index for each loop.
results{i}{m} = L_nodo;

10 Comments

thank you for your reply Bob. Exactly, the results are only recorded for the internal loop, i tried to debug it. The indexing you suggested is what i need but for the problem you already mentioned i have the same results for each cells. Do you have suggestions? How can i modify the code? Now is like this:
clc; clear; close all;
load ('sclerometrica_equotip_v1')
misure = [ID_stop L Nn Bn];
% misure(any(isnan(misure), 2), :) = [];
ID_stop1 = misure(:,1);
L1 = misure(:,2);
Nn1 = misure(:,3);
Bn1 = misure(:,4);
k = unique(ID_stop1);
result = cell(1, numel(k));
for i = 1:numel(k)
figure(i)
index = (ID_stop1 == k(i));
N= Nn1(index); %battute nodi
j = unique(N);
for m = 1:numel(j)
index2 = (N == j(m));
L_nodo = L1(index2);
result{i}{m} = L_nodo;
end
end
Really thanks
If you are running two loops, and getting the same set of values for each internal loop it means you either have multiple sets of the same data, or you are not drawing in the change from the outer loop. This is usually done with indexing, but it could also be some calculation contained within the outer loop that flows into the inner loop.
I have not looked at your data, so I can't say with certainty, but I suspect it is because you also need to look at indexing L_nodo for both loops, or some other portion of that calculation. It is also possible that you have an error in the calculation of N.
i'm new in Matlab so i don't know how to solve it. i need the second loop for the second indexing but i also need to come back to the previous loop to get the other results and i don't know how to do. Can you have a look to my data? I will appreciate it a lot
Unfortunately, no, I cannot look at the data specifically. It's nothing personal, just that my sense of internet security refuses to let me compromise on this.
I'm going to try to break down the problem description you originally posted to see if I can figure things out in more detail. Let me know if I have not interpreted something correctly.
ID_stop1 is an identifier of data and it is repeated 80 times for each value.
k = unique(ID_stop1);
So ID_stop1 is large, and has a number of unique values which you have identified with k. Within ID_stop1 each of those unique values should be repeated 80 times, so 'index' is a logic array which identifies which ID_stop1 elements are equal to a specific unique value.
index = (ID_stop1 == k(i));
Nn1 is a number from 1 to 20 and it is still repeated for each unique value of ID_stop1.
So, Nn1 has the same number of elements as ID_stop1, but only contains integers from 1:20? I assume this is the case because identifiying specific elements of Nn1 with index hasn't seemed to return any errors, so they must be the same size.
N= Nn1(index);
This means that N should contain 80 elements with values from 1:20. Will N always contain all integers, or is it possible that some integers may not be present? If all integers are always present, then setting j = uniques(N) is unnecessary because you already know all possible values (1:20).
index2 = (N == j(m));
index2 is an 80x1 logic array which points to all elements of N which correspond to a specific integer.
L_nodo = L1(index2);
L_nodo is the values of L1, which must also be 80x1, which relate to the specific integer from earlier.
If all of this is correct I would suggest putting a debug marker on the calculation for N to see if it changes at all with each loop. I suspect you may have some kind of repeating pattern, and N is not actually changing. Check the link if you are unsure of how to debug a code.
i'm really glad about your patience :)
everything you wrote is correct. Now i have debugged the code and i've seen values of L_nodo changing in the second loop so it is correct. The loop starts with i= 1. When the second loop is finished, it wrote into result only this data. It should come back to the level of the first loop and run it again for the others i and write the values in the cell array results again.
Are you saying everything is good then, or is it still not producing the correct final output?
it is still not producing the rigth output.. i don't know how to let it come back to the first loop when the second is completed...
It should do this automatically. Your internal for loop is fully contained within the outer for loop, so by definition of a for loop, the outer will run the entire inner loop for each iteration of outer index.
Are you positive that it is not running? Where did you place your debug marker?
maybe there is something wrong with the indexing... i put a debug marker in the beginning of the first for loop
i solved the problem, now it works good!!
k = unique(ID_stop1);
for i = 1:numel(k)
index = (ID_stop1 == k(i));
nn = Nn1(index);
bn = Bn1(index);
ll = L1(index);
j =unique(nn);
for ii= 1:numel(j)
index1 = (nn == j(ii));
Lnodo = ll(index1);
result{i}{ii} = Lnodo;
end
end
Thank you for your suggestions!!! :)

Sign in to comment.

More Answers (0)

Asked:

on 6 Mar 2019

Commented:

on 6 Mar 2019

Community Treasure Hunt

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

Start Hunting!