Create a new array over each iteration
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Riccardo Tronconi
el 14 de Jun. de 2021
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 comentarios
Stephen23
el 15 de Jun. de 2021
"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.
Respuesta aceptada
Rik
el 14 de Jun. de 2021
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')
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{1}
4 comentarios
Rik
el 16 de Jun. de 2021
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!