Multidimensional array to individual variables - arrays

I have a multidimensional array like this, which is a set of latitudes and longitudes.
multi = [[12.96145, 77.58408],[12.96219, 77.58447],[12.96302, 77.58489],[12.96316, 77.58496],[12.96348, 77.58511],[12.96356, 77.58512],[12.96363, 77.5851],[12.96372, 77.58506],[12.96376, 77.58497],[12.96374, 77.58479]]
I want to convert the above array into individual variables like below:
lat1 = 12.96145
lon1 = 77.58408
lat2 = 12.96219
lon2 = 77.58447
.
.
I tried to achieve the above by a lengthy procedure like below. And I hope there will be definitely a better way to do this.
l1,l2,l3,l4,l5,l6,l7,l8,l9,l10 = multi
lat1 = l1.first
lon1 = l1.last
lat2 = l2.first
lon2 = l2.last
.
.

lat1, lon1,
lat2, lon2,
...
latn, lonn,
= multi.flatten
or
(lat1, lon1),
(lat2, lon2),
...
(latn, lonn),
= multi

Also may assign values to instance variables like this:
multi.each_with_index do |pair, i|
instance_variable_set("#lat#{i%multi.size+1}", pair.first)
instance_variable_set("#lon#{i%multi.size+1}", pair.last)
end
Then refer to variables so: #lat1, #lon1, ..., #latn, #lonn

Related

Storing numpy.ndarrays from a loop

I am trying to store the numpy.ndarrays defined as x_c, y_c, and z_c for every iteration of the loop:
for z_value in np.arange(0, 5, 1):
ms.set_current_mesh(0)
planeoffset : float = z_value
ms.compute_planar_section(planeaxis = 'Z Axis', planeoffset = planeoffset)
m = ms.current_mesh()
matrix_name = m.vertex_matrix()
x_c = matrix_name[:,0]
y_c = matrix_name[:,1]
z_c = matrix_name[:,2]
I would like to be able to recall the three arrays at any z_value, preferably with reference to the z_value i.e x_c # z_value = 2 or similar.
Thanks for any help!
p.s very new to coding, so please go easy on me.
You have to store each array in an external variable, for example a dictionary
x_c={}
y_c={}
z_c={}
for z_value in np.arange(0, 5, 1):
ms.set_current_mesh(0)
planeoffset = float(z_value)
ms.compute_planar_section(planeaxis = 'Z Axis', planeoffset = planeoffset)
m = ms.current_mesh()
m.compact()
print(m.vertex_number(), "vertices in Planar Section Z =", planeoffset)
matrix_name = m.vertex_matrix()
x_c[planeoffset] = matrix_name[:,0]
y_c[planeoffset] = matrix_name[:,1]
z_c[planeoffset] = matrix_name[:,2]
Please, ensure you call m.compact() before accessing the vertex_matrix or you will get a MissingCompactnessException error. Please, note that it is not the same to store anything in x_c[2] or in x_c[2.0], so choose if your index has to be integers o floats and keep the same type (in this example, they are floats).
Later, you can recall values like this:
print("X Values with z=2.0")
print(x_c[2.0])

How to make a spatial plot within desired level of values of a variable from netcdf file using matlab

I could able to interpolate and plot chlorophyll concentration with respective x ,y. Now I need to display within a specific range of values. For example value between 0 and 10 as in the code below
ncfile_1 = 'A20150322015059.L3m_MO_CHL_chlor_a_4km.nc';
lat_1 = ncread(ncfile_1,'lat') ;
lon_1 = ncread(ncfile_1,'lon') ;
chlor_a_1 = ncread(ncfile_1,'chlor_a');
[X_1,Y_1] = meshgrid(lon_1,lat_1) ;
xi_1 = linspace(30,100,1000) ;
yi_1 = linspace(0,30,1000) ;
[Xi_1,Yi_1] = meshgrid(xi_1,yi_1);
iwant_1 = interp2(X_1,Y_1,chlor_a_1',Xi_1,Yi_1)' ;
pcolor(xi_1,yi_1,iwant_1'); shading interp;
c = colorbar;
cmap = jet(255);
cmap(:,3) = 0;
colormap(cmap)
caxis([0, 10])
I could plot figure but the levels in the plot seems wrong. Please point out the mistake in my code.
I have tried with this code but doesn't work
if chlor_a_1>=0 && chlor_a_1<=10
disp(chlor_a_1);
[Xi_1,Yi_1,chlor_a_1] = peaks;
plot(Xi_1,Yi_1,chlor_a_1,49)
I am a beginner on Matlab. Any help is highly appreciated.

How to transpose a vector of strings in Julia 0.6 (in order to plot multiple legends)?

In Julia 0.5 you can write:
using DataFrames, Plots, StatPlots
df = DataFrame(
fruit = ["orange","orange","orange","orange","apple","apple","apple","apple"],
year = [2010,2011,2012,2013,2010,2011,2012,2013],
production = [120,150,170,160,100,130,165,158],
consumption = [70,90,100,95, 80,95,110,120]
)
plotlyjs()
mycolours = [:green :orange]
legend1 = sort(unique(df[:fruit]))
legend2 = legend1'
fruits_plot = plot(df, :year, :production, group=:fruit, linestyle = :solid, linewidth=3, label = ("Production of " * legend2), color=mycolours)
where legend1 is a 2-element DataArrays.DataArray{String,1} and legend2 is a 1×2 DataArrays.DataArray{String,2}.
Now, in julia 0.6 legend2 = legend1' is not working any more. You can do instead a legend2 = reshape(legend1, (1, :)), but that produces a pretty obscure 1×2 Base.ReshapedArray{String,2,DataArrays.DataArray{String,1},Tuple{}} that then is not accepted in the plot() call.
So, any way in julia 0.6 to produce from a 2-element DataArrays.DataArray{String,1} a 1×2 DataArrays.DataArray{String,2} ?
Again, posting on SO helps.. ;-)
I finally got that I can obtain the plot anticipating the string concatenation:
fruits_plot = plot(df, :year, :production, group=:fruit, linestyle = :solid, linewidth=3, label= reshape("Production of " * sort(unique(df[:fruit])),(1,:)), color=mycolours)

How to assign different labels to a set of classes using Matlab?

Can someone guide me to do this in the best possible way using matlab.
i have files with names 001_g_01.sig...001_g_08.sig, 002_g_01.sig...002_g_010.sig, 003_g_01.sig...003_g_08, upto n files.
what i want to do is assigning labels to files belong to one user (i.e.001_g_01.sig...001_g_08.sig as "User1", 002_g_01.sig...002_g_010.sig as "User2" and so on).
i have the following code to read the files.
dirName= '/FolderPath';
files = dir( fullfile(dirName,'001_g_1.Sig') );
files = {files.name};
dirName1= '/FolderPath';
files1 = dir( fullfile(dirName1,'*.Sig') );
files1 = {files1.name};
for i=1:length(files)
fname = fullfile(dirName,files{i});
%# some calculation
for j=i+1:length(files1)
fname1 = fullfile(dirName1,files1{j});
%# some calculation
end
end
files1 =
'001_g_01.sig'
'001_g_08.sig'
'003_g_01.sig'
.
.
If files1 is a cell like above, then you can write:
userlabel = {'001','002','003'};
f = cellstr(files1);
for ii = 1:3
result{ii} = strncmp(userlabel(ii),f,3);
end
You can do it in this way:
label = [repmat('User1',size(files,1),1); repmat('User2',size(files1,1),1) ];
This will create a array of labels containing 'User1' and 'User2'. If you need it separately, you can do it:
label1 = [repmat('User1',size(files,1),1)];

How to use arrays created by loop? Matlab

The code I'm using imports data from multiple files and saves them into an array of cells, the code is as follows:
[FileName,PathName,FilterIndex] = uigetfile('*.txt*','MultiSelect','on');
numfiles = size(FileName,2);
FileData= cell(1,numfiles);
for ii = 1:numfiles
FileName{ii};
A=[];
entirefile =fullfile(PathName,FileName{ii});
fid = fopen(entirefile);
tline = fgets(fid);
while ischar(tline)
parts = textscan(tline, '%f;');
if numel(parts{1}) > 0
A = [ A ; parts{:}' ];
end
tline = fgets(fid);
end
fclose(fid);
FileData{ii} = A;
A = FileData{ii};
X = A(:,1);
Y = A(:,5);
DataToUse = [X,Y];
end
Now my issue is I want to use the first DataToUse created by the loop, which will be data from the first file, seperatley to the other files but I can not issolate it. I have tried DataToUse(1), DataToUse(1,1) and DataToUse(:,[1,2]) but none are working for me. An example of the type of data would be:
DataToUse=
0.0762 0.0271
0.0763 0.2671
0.0764 0.4079
0.0765 0.0510
0.0766 0.0087
0.0767 0.0099
0.0768 0.0067
0.0769 0.0047
0.0770 0.0047
0.0771 0.0349
0.0772 0.2094
0.0773 0.2740
0.0774 0.0294
0.0775 0.0100
0.0776 0.0159
I have different numbers of this kind of data depending on how many files are selected but I would like to only use the first initially and use the others later. Anybody know how I can go about doing this? Many thanks in advance
The solution is to use cell arrays, like so:
DataToUse{ii} = [X, Y]
To get the desired output put this after your for-loop:
firstLoopXY = DataToUse{1}
Enjoy!

Resources