how to iterate cell array?
Show older comments
hello !
i have a cell array N1 (100x1),attached here, i want to do 51 iteration for each element of N1(each matrix).
knowing that N1 was saved and i just load it.
i tried with different method but always the same error
for i = 1:51
i = i+1
for itr = 1:100 %number of iteration is the size of my cell array
N = all_N1{i,itr}; % here is error " Index in position 2 exceeds array bounds. Index must not exceed 1."
%...............code
end
end
any help is appreciated!
Answers (3)
Walter Roberson
on 26 Feb 2023
0 votes
You say that N1 is 100 x 1.
You have iter = 1:100 so on the second round, iter will become 2.
You have all_N1{i,iter} but iter has become 2 on the second iteration. But your cell array is 100 x 1 so there is no second column. Not unless you assigned into all_N1{:,2} inside the for i loop.
2 Comments
Majid
on 26 Feb 2023
Walter Roberson
on 26 Feb 2023
N1 is already saved as 100 x 1. When your for itr becomes 2, you attempt to access all_N1{i,iter} which would be all_N1{i,2} . If N is the same thing as all_N1 then you have a problem.
for i = 1:51
for itr = 1:100 %number of iteration is the size of my cell array
N = all_N1{itr,i}; % switch position of indices.
end
end
As you say, N1 is of size 100x1, you need to switch the position of indices in the inner for loop as above. Also, you don't require to increment the outer for loop index, as this will cause the index to exceed out of bounds i.e. > 51 end
3 Comments
Majid
on 26 Feb 2023
clear all % add this at the beginning of code
M = {rand(100,1)} % some data
N1 = repmat(M,100,1) %each N1 is 100 x 1
all_N1 = repmat(N1,1,51) % assuming all N1
for i = 1:51 %
for itr = 1:100 %number of iteration is the size of my cell array
N = all_N1{itr,i}; %
end
end
N
Its unlikely you would get such error, Can you check whether you cleared all variables in your workspace ?each time you begin to run code, otherwise the workspace variables get populated and exceed the array dimensions,
Rik
on 4 Mar 2023
Why suggest clear all? Using a function would also provide a clean slate.
Note that clear or clearvars should be preferred, as clear all clears much more. It is almost equivalent to restarting Matlab every time you run the code.
Torsten
on 26 Feb 2023
all_N1 = cell(51,1);
% Generate 100 matrices of size 20x20 and save them in cell array
for i = 1:100
all_N1{i} = rand(20);
end
% Use the matrices in iterations
for i = 1:100
M = all_N1{i};
for iter = 1:51
% do something with the matrix M
end
end
29 Comments
Majid
on 26 Feb 2023
Walter Roberson
on 26 Feb 2023
step = load('all_N1.mat');
all_N1 = step.all_N1;
for iter = 1:100
this_N = all_N1{iter};
for i = 1 : 51
N = this_N;
%my code
end
end
this now does 51 iterations with the same array for each iteration. It is not clear why this would be desired.
Majid
on 26 Feb 2023
So you mean:
step = load('all_N1.mat');
all_N1 = step.all_N1;
all_N1_iter = cell(100,51);
for iter = 1:100
this_N = all_N1{iter};
all_N1_iter{iter,1} = this_N;
for i = 2 : 51
% do something with the matrix this_N
all_N1_iter{iter,i} = result of iteration i;
end
end
?
Majid
on 27 Feb 2023
Rik
on 27 Feb 2023
Then what you wrote didn't actually depend on i. Since you didn't show that code we can't explain to you why.
Majid
on 27 Feb 2023
And where exactly are you storing the results back to all_N1? You have not really shown that your code depends on the value of i, but if you say that the result is the same, then apparently it doesn't.
Note that this can happen due to the inherent nature of the problem:
A = ones(1,4);
for n=1:numel(A)
A(n) = 1-(n/n); % code clearly depends on n
end
disp(A) % result is the same for each value of n
Majid
on 27 Feb 2023
Rik
on 27 Feb 2023
You don't have 51 columns, you only have one. You can duplicate your data, but then the results will be identical. What are you trying to do? That is the most important question here. In the code of your previous comment, your calculations do not actually depend on the value of i or f_in.
Majid
on 27 Feb 2023
Rik
on 27 Feb 2023
So then you end up with an array of 100x51 elements for fval. I still don't get what your problem is. You have a cell array with 100 elements. Each of those elements will result in 51 output values based the value of f_in. The indexing is now fixed, but your complaint that all columns contain the same data isn't.
Is that indeed what remains of this problem?
Majid
on 27 Feb 2023
Torsten
on 27 Feb 2023
[[MyResult{i, iter},fval{i,iter}]= solve(prob)
Doesn't [[ throw an error ?
My guess is that you either solve the same problem 51 times or that all your 51 problems have the same solution.
Majid
on 27 Feb 2023
Torsten
on 27 Feb 2023
step = load('all_N1.mat');
all_N1 = step.all_N1;
all_N1_copied = cell(size(all_N1,1),51);
for i = 1:size(all_N1,1)
for j = 1:51
all_N1_copied_51_times{i,j} = all_N1{i};
end
end
Rik
on 27 Feb 2023
You're proposing to copy your data, but that will get you the same results.
How are you making sure that a different value of f_in will result in a different output? It seems to me that the only code that was relevant is the code you removed.
Majid
on 27 Feb 2023
Rik
on 27 Feb 2023
You told us you didn't show all code because it would be confusing, which is fine.
What you should do is this:
output = cell(numel(all_N1),1)
for iter = 1:numel(all_N1) % size of N1
for f_in = 0:50 % this a variable that i will use in my code
i = f_in+1;
output{iter,i} = YourCustomFunction(all_N1{iter},f_in);
end
end
function output = YourCustomFunction(N,varargin)
% Here you can put all your other code
end
This way you can really isolate everything. This way you can make 100% sure that your code depends only on the data for 1 iteration. You can add inputs and outputs as needed, but you should not put anything else inside the loops.
Majid
on 2 Mar 2023
Then you know that your code doesn't actually depend on the value of f_in. Just like the code I posted previously:
A = ones(1,4);
for n=1:numel(A)
A(n) = 1-(n/n); % code clearly depends on n
end
disp(A) % result is the same for each value of n
That isn't bad, it is what it is. But no matter how you package and compartmentalize your code, this will be the case. If you expected something else there are two options: either your expectations are wrong, or your code is wrong. Since you have posted neither, it is fully up to you to decide which it is. But at least now you know. If you share your implementation of YourCustomFunction, we might be able to help you find out why the result doesn't depend on f_in.
Rik
on 3 Mar 2023
If you did as you claimed, the problem is not in any of the loops.
You need to share your implementation of Your Custom Function, otherwise there is no point in responding.
Rik
on 3 Mar 2023
So you didn't do what I suggested. Why exactly didn't you do that? You should have implemented it something like I did below.
% Load data from mat file
step = load('all_N1.mat');
all_N1 = step.all_N1;
% Prepare output parameters
MyResult = cell(1,numel(all_N1));
fval = cell(1,numel(all_N1));
for iter = 1:numel(all_N1)
for f_in = 0:50
i = f_in+1;
% Run solver
[MyResult{i,iter},fval{i,iter}, exitflag] = YourCustomFunction(all_N1{iter},f_in);
% Overwrite fval when solver fails.
if exitflag<=0
fval{i,iter}=-inf;
end
end
end
function [MyResult,fval,exitflag] = YourCustomFunction(N,f_in)
S = optimvar("S",[M,1],"Type","integer","LowerBound",0,"UpperBound",1);
obj = fcn2optimexpr( @Objective_function ,S);
prob = optimproblem('ObjectiveSense','max');
prob.Objective = obj ;
constr1 = S <= Q./sum(fcn2optimexpr( @Myconstraint1, q_int),2 );
constr2 = cumsum(S.*(sum(Energy_Pr,2))) <= E_max(1)-E_prop;
prob.Constraints.constr1 = constr1;
prob.Constraints.constr2 = constr2;
[MyResult,fval, exitflag] = solve(prob);
end
Now you need to show me where exactly you're using f_in (or even N). I only see M, along with some undefined variables and functions you haven't shared.
Do you see how this helps defining the problem? Do you also notice how the part with the fewest comments is the part that is causing the issue? That should tell you something.
Rik
on 3 Mar 2023
Ok, now you replaced q_int with f_in. That helps, but since it still doesn't use N, I doubt that actually solves the problem.
I would suggest you write a complete implementation of YourCustomFunction. Don't edit any of the rest of my code. Only that function. You can add extra output variables if you need to, but do not change anything else. That will help you narrow down what actually is going on.
Some problems are just too big to fit in your head at the same time. That happens to me all the time. That is why I split up big problem into smaller ones. I'm not smart enough to solve 1 big problem, but I can generally manage to solve 20 smaller ones.
Majid
on 3 Mar 2023
Rik
on 3 Mar 2023
I'm sorry, I'm out. I can't help you if you refuse to follow my advice for troubleshooting.
I hope fo you someone else will be able to pick up this thread, but I'm out.
If you change your mind you can attach the relevant m files and show your implementation of YourCustomFunction.
You have been working on this problem for months now.
In the meantime, did you spend the 2 hours to pass the MATLAB tutorial ?
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!