Create a new array over each iteration

Hi guys! Starting from one table (A) I would divide it in n-table according to the max value of the A(:,4). At this point every each iteration I must create another table as it follows:
function [T_num2str(i)] = par(A)
n= max(A{:,4});
for i=1:n
search = find(A{:,4}==i);
eval(['T_' num2str(i) ' = table;']);
T_num2str(i) = position(search,:);
end
end
I have two problem:
1- How to define output values if I do not know how many they would be?
2- How to solve this indexing problem ?
T_num2str(i) = position(search,:);

7 Comments

This is an example of the XY problem. Why don't you explain what you want to do? Your use of eval alone already suggests there is probably a better solution.
My guess would be that a solution would involve a comma separated list:
c=cell(1,3);
[c{:}]=ndgrid(1:2,2:3,4);
format compact,celldisp(c)
c{1} = 1 1 2 2 c{2} = 2 3 2 3 c{3} = 4 4 4 4
How exactly you can use this concept for your problem, depends on what you actually want to do.
According to ex. in the fouth coloumn I have numbers from 1 to 3. For this purpose, I have created an if statement in the fouth coloum and once is true I have to create a new array (mx6) in which I store each rows .
It is not clear to me what you want to have as an output variable. Can you give a hard-coded example?
%for example:
data_I_want=...
[2.693613716 2.652678399;
3.577557088 3.49910799;
2.56422065 2.604867068;
3.573647731 3.550236118;
2.661032734 2.607972076;
3.541105827 3.585182487];
data_I_want1=
[ Location 1.61E+18 lab 1 2.693614 3.577557
Location 1.61E+18 lab 1 2.652678 3.49910799
Location 1.61E+18 lab 1 2.666976 3.5399065
Location 1.61E+18 lab 1 2.620591 3.50225475 ]
data_I_want2=
[ Location 1.61E+18 lab 2 2.564221 3.57364773
Location 1.61E+18 lab 2 2.604867 3.55023612
Location 1.61E+18 lab 2 2.699815 3.53594614
Location 1.61E+18 lab 2 2.614945 3.58736603 ]
Same for 3
Why do you want numbered variables? They force you to write difficult and buggy code.
I need it parametric so Its functioning is still valid if max(indices) is either lower or greater.
"I need it parametric so Its functioning is still valid if max(indices) is either lower or greater. "
Sure, but you did not answer Rik's question.
So far there is no obvious reason why you cannot use simpler indexing, rather than your complex and inefficient approach.

Sign in to comment.

 Accepted Answer

The code below gives you what you want. The only difference is that you need to write my_data{1} instead of my_data1.
[~,~,data]=xlsread('ex[1].xlsx')
data = 12×6 cell array
{'Location'} {[1.6149e+18]} {'lab'} {[1]} {[2.6936]} {[3.5776]} {'Location'} {[1.6149e+18]} {'lab'} {[2]} {[2.5642]} {[3.5736]} {'Location'} {[1.6149e+18]} {'lab'} {[3]} {[2.6610]} {[3.5411]} {'Location'} {[1.6149e+18]} {'lab'} {[2]} {[2.6049]} {[3.5502]} {'Location'} {[1.6149e+18]} {'lab'} {[3]} {[2.6080]} {[3.5852]} {'Location'} {[1.6149e+18]} {'lab'} {[3]} {[2.6523]} {[3.5085]} {'Location'} {[1.6149e+18]} {'lab'} {[3]} {[2.6343]} {[3.5355]} {'Location'} {[1.6149e+18]} {'lab'} {[2]} {[2.6998]} {[3.5359]} {'Location'} {[1.6149e+18]} {'lab'} {[2]} {[2.6149]} {[3.5874]} {'Location'} {[1.6149e+18]} {'lab'} {[1]} {[2.6527]} {[3.4991]} {'Location'} {[1.6149e+18]} {'lab'} {[1]} {[2.6670]} {[3.5399]} {'Location'} {[1.6149e+18]} {'lab'} {[1]} {[2.6206]} {[3.5023]}
indices=cell2mat(data(:,4));
my_data=cell(max(indices),1)
for n=1:max(indices)
my_data{n}=data(indices==n,:);
end
my_data
my_data = 3×1 cell array
{4×6 cell} {4×6 cell} {4×6 cell}
my_data{1}
ans = 4×6 cell array
{'Location'} {[1.6149e+18]} {'lab'} {[1]} {[2.6936]} {[3.5776]} {'Location'} {[1.6149e+18]} {'lab'} {[1]} {[2.6527]} {[3.4991]} {'Location'} {[1.6149e+18]} {'lab'} {[1]} {[2.6670]} {[3.5399]} {'Location'} {[1.6149e+18]} {'lab'} {[1]} {[2.6206]} {[3.5023]}

4 Comments

Another question: How can I manage values inside for instance my_data{1}?
To indexing for example in a for loop
"How can I manage values inside for instance my_data{1}?"
What do you mean "manage"?
You can access the arrays using indexing, exactly as you show. For example, my_data{1} is the first array. you can do anything with that array, just like you would any other array.
It is not clear therefore what your question is.
for i=1:length(my_data{1})
if my_data{1}{i,5} >= 2
% do some calculation
end
end
Doing so It returns me an error
You made the mistake of using length and assuming it would return the number of rows. The length function does not guarantee that. Use size(my_data{1},1) instead if you want the number of rows. You might also want to learn about the numel function if you want to loop over all elements of an array.

Sign in to comment.

More Answers (0)

Asked:

on 14 Jun 2021

Edited:

Rik
on 16 Jun 2021

Community Treasure Hunt

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

Start Hunting!