MATLAB Answers

How to solve pre-allocating array?

2 views (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 18 Jan 2020
Edited: Stephen Cobeldick on 18 Jan 2020
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 Comments

hanif hamden
hanif hamden on 19 Jan 2020
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.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2015a

Translated by