Create a new array over each iteration
조회 수: 12 (최근 30일)
이전 댓글 표시
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
Stephen23
2021년 6월 15일
"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.
채택된 답변
Rik
2021년 6월 14일
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
Rik
2021년 6월 16일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!