How to solve pre-allocating array?
Show older comments
Hello everyone,
I have a coding like this, where I need to extract the row which has same index number in column 5 of my data. Attached is my data,
%Example matrix
A = load('Ntxp0014.txt');
%Find indices to elements in first column of A that satisfy the equality
ind11 = A(:,5) == 11 ;
ind12 = A(:,5) == 12 ;
ind13 = A(:,5) == 13 ;
ind14 = A(:,5) == 14 ;
ind15 = A(:,5) == 15 ;
ind16 = A(:,5) == 16 ;
ind17 = A(:,5) == 17 ;
ind18 = A(:,5) == 18 ;
ind19 = A(:,5) == 19 ;
ind20 = A(:,5) == 20 ;
ind21 = A(:,5) == 21 ;
ind22 = A(:,5) == 22 ;
%Use the logical indices to index into A to return required sub-matrices
A11=A(ind11,:);
A12=A(ind12,:);
A13=A(ind13,:);
A14=A(ind14,:);
A15=A(ind15,:);
A16=A(ind16,:);
A17=A(ind17,:);
A18=A(ind18,:);
A19=A(ind19,:);
A20=A(ind20,:);
A21=A(ind21,:);
A22=A(ind22,:);
but it is not practical when deals with many data. How I would to solve this pre-allocating array? However, I've try using for ... end function but still have error.
%Example matrix
A = load('Ntxp0014.asc');
%Find indices to elements in first column of A that satisfy the equality
for i = 11:364
ind(i) = A(:,5) == i ;
%Use the logical indices to index into A to return required sub-matrices
A(i) = A(ind(i),:);
end
Accepted Answer
More Answers (0)
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!