# Why is my loop only running 217 times when I expect it to run 649 times? See the script below

1 view (last 30 days)
Sabelo Reliable on 24 Aug 2017
Commented: Sabelo Reliable on 24 Aug 2017
I have simulated reflectance (100 iterations) and compared it to the measured spectra (649 samples) using RMSE. Since each simulation was compared to every measured spectrum this generated a 100x649 table. Now I need to find the lowest RMSE in each of the 649 columns. Every time I m trying to find the minimum RMSE this error message pops up: Subscripted assignment dimension mismatch. I expected p to 649 but its 218 and as result the script predict Cab and LAI only up to 217th column. Below is my script and please help me identify the problem
m = 7x649
for p=1:length(m(1,:))
[row(:,p),col(p)]=find(rmse==min(rmse(:,p)));%finds minimum RMSE
predicted_LAI(:,p)=LAI(row(p));%LAI
prdiected_Cab(:,p)=Cab(row(p));%Cab
corress_refle(:,p)=rdot(row(p));% reflectance
end
When I change the for loop it runs without any error but predicts LAI and Cab only for the 649th column while the rest are assigned zeros.
for p=length(m(1,:))
[row(:,p),col(p)]=find(rmse==min(rmse(:,p)));%finds minimum RMSE
predicted_LAI(:,p)=LAI(row(p));%LAI
prdiected_Cab(:,p)=Cab(row(p));%Cab
corress_refle(:,p)=rdot(row(p));% reflectance
end
##### 2 CommentsShowHide 1 older comment
Sabelo Reliable on 24 Aug 2017
I am actually simulating reflectance using PROSAIL model. LAI (100x1) and Cab (100x1) are amongst the variables that were used to simulate reflectance. With this script I am trying to find the simulated spectra that has a low RMSE when compared to measured spectra. Once the simulated spectra with low RMSE has been found I would need to know the variables (LAI, Cab) which makes up that simulation. Presently this is not running across all 649 columns but only 217 columns.

Guillaume on 24 Aug 2017
Edited: Guillaume on 24 Aug 2017
The whole script does not make much sense:
for p = 1:length(m(1,:))
First thing: forget about length, it's not a good function and often used improperly. Use size for matrices, numel for vectors. Much better and faster
for p = 1:size(m, 2)
Next,
[row(:,p),col(p)]=find(rmse==min(rmse(:,p)));
The only reason one would use find(x == min(x)) is if one expect that the minimum value is repeated several times. If not,
[~, index] = min(x)
is much simpler. So, assuming that there are repetitions of the minimum values, the find will return several locations. The rows are assigned to row(:, p). Several things can happen there:
• row does not yet exist. It is created to have as many rows as elements returned by find. All is fine.
• row does exist and has the same numbers of rows as the number of elements returned by find. The new values are put into column p of row. All is fine.
• row already exists but find return more or less elements than its number of rows. The assignment will fail with dimension mismatch error. Considering that there is no guarantee that for each p you'll have the same number of minimum elements, you're bound to hit this error.
Going back to find, the columns are assigned to col(p) which can only receive one element. Whenever find returns more than one, you'll get a dimension mismatch error.
Next,
xxx = yy(row(p));
So, before we had row(:, p), now we have row(p). You're not consistent and not even accessing the values you've just stored in row. This makes zero sense.
I have no idea what your real intent is, you haven't said what the code is supposed to do and you did not comment the code (hint!), so it's impossible to say what the correct code should be.
Sabelo Reliable on 24 Aug 2017
I am actually simulating reflectance using PROSAIL model. LAI (100x1) and Cab (100x1) are amongst the variables that were used to simulate reflectance. With this script I am trying to find the simulated spectra that has a low RMSE when compared to measured spectra. Once the simulated spectra with low RMSE has been found I would need to know the variables (LAI, Cab) which makes up that simulation. Presently this is not running across all 649 columns but only 217 columns.
The assumption that there are several minimum is correct. Note that after computing RMSE between the simulated and measured spectra I get a 100x649 table. In each of the 649 columns I need to go through the rows and find the minimum RMSE. So the loop that read as
for p = 1:length(m(1,:))
was meant to assigned the lowest RMSE in each column (there are 649 columns and only 217 were assigned minimum RMSE).I have since changed this to read as
for p = 1:size(m, 2)
Now the script is running without producing errors but it only find the value of the 649th column and the other columns are filled with zeros.

njj1 on 24 Aug 2017
In your second script you assign the counter variable 'p' to be a single value, length(m(1,:)), which is simply 649. So Matlab computes the value for the 649th entry, and fills the previous entries with zeros.
Sabelo Reliable on 24 Aug 2017
That was a second attempt to resolve the stated problem. Earlier on I wrote the code differently and it ran up to the 217th entry. Is there a way I can work this out?