How to solve pre-allocating array?

조회 수: 1 (최근 30일)
hanif hamden
hanif hamden 2020년 1월 18일
댓글: Stephen23 2020년 1월 19일
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

채택된 답변

Stephen23
Stephen23 2020년 1월 18일
편집: Stephen23 2020년 1월 18일
Using numbered variables is a sign that you are doing something wrong.
Accessing numbered variables is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:
You can easily avoid that bad code approach by splitting that data into a cell array, either using a simple loop or some other grouping function, for example using arrayfun:
>> A = dlmread('Ntxp0014.txt'); % better than LOAD
>> V = 11:22;
>> F = @(n) A(A(:,5)==n,:);
>> C = arrayfun(F,V,'UniformOutput',false);
Lets check the contents of the first cell (fifth column==V(1)==11):
>> C{1}
ans =
1.9921e+013 1.84720 95.016 -36.904 11 14
1.9921e+013 1.79460 95.034 -36.599 11 14
1.9921e+013 1.74200 95.053 -36.346 11 14
1.9921e+013 1.68940 95.072 -36.130 11 14
1.9921e+013 1.63680 95.091 -35.879 11 14
... lots of lines
1.9921e+013 0.216460 95.597 -28.778 11 14
1.9921e+013 0.163860 95.616 -28.799 11 14
1.9921e+013 0.111250 95.635 -28.807 11 14
1.9921e+013 0.058644 95.653 -28.891 11 14
1.9921e+013 0.006037 95.672 -28.904 11 14
  댓글 수: 2
hanif hamden
hanif hamden 2020년 1월 19일
편집: hanif hamden 2020년 1월 19일
clc;clear all;close all;
% Input matrix
A = dlmread('Ntxp0014.txt');
% Identify and extract each row according to cycle
V = 11:364;
F = @(n) A(A(:,5)==n,:);
C = arrayfun(F,V,'UniformOutput',false);
% Reference LatLon of Cycle 11
ref_latC1 = C{1}(:,2);
ref_lonC1 = C{1}(:,3);
latlon1 = [ref_latC1 ref_lonC1];
% LatLon of Cycle 12 to 364
for j=22 %2-354 <-I have to run one by one
obs_latC2 = C{j}(:,2);
obs_lonC2 = C{j}(:,3);
latlon2 = [obs_latC2 obs_lonC2];
%Extract minimum distance point by point
for k= 1:length(latlon2)
[d1km(k)]=lldistkm(latlon1(1,:),latlon2(k,:));
Dist1 = d1km';
end
B2 = [C{j} Dist1];
ind2=B2(:,7)== min(B2(:,7));
B2 = B2(ind2,:);
%Append the data with ref point
%Append1 = [C{1}(1,:) 0 ; B2]
Append1 = [B2]
fid = fopen('txp0014_pt1.txt','a');
fprintf(fid,'%17.2f \t %.6f \t %.6f \t %.3f \t %.0f \t %.0f \t %.4f\n',Append1.');
fclose(fid);
end
Thank you sir for the previous solution. I've further my coding and look like this. I would to extract each cycle and append it all. since my coding works manually as I have to keep change the number of j=2, run, j=3, then run again. Could you help me sir if possible? if I put j=2:354, there will be an error.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by