Resize array, prevent boundary effect
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/196222/image.png)
I want to write a script that can double the size of an array using part of the data, while the original array remains the center of the big array. I think selecting some part and flipping it will be a good idea. I wrote the following script, but need assistance on how to assemble the left, right, top, bottom and center of the array to create the double size. I will greatly appreciate help to achieve this.
% read single column data and reshape
bouin='bou.dat'; % in data
numcolumns=200; % nx
numrows=200; % ny
fid=fopen(bouin,'r');
bou=fscanf(fid,'%f\n',[numcolumns numrows]); %open, read in and store
fclose(fid);
% preallocate array with double size of the intial data
bou_doub=zeros(2*numcolumns 2*numrows);
% define the boundary layers to be added
tempa=flipud(bou_doub(1:(numcolumns/2) 1:numrows)); % upper boundary
tempb=flipud(bou_doub((numcolumns/2):numcolumns 1:numrows)); % lower boundary
tempc=fliplr(bou_doub(1:numcolumns 1:(numrows/2))); % right boundary
tempd=fliplr(bou_doub(1:numcolumns (numrows/2):numrows)); % left boundary
%write all the parts into the main array with double size
bou_out=bou_doub(
% reshape final array to single column
bou_final=reshape(bou_out(numcolumns*numrows,1));
% write final array to file
fid=fopen('bou_final', 'w');
fprintf(fid,'%6.2f\n',bou_final); %this opens and writes the output matrix (adding the mean gravity value substracted prior to the FFT) in a ASCII file with a single data column with gravity anomaly
fclose(fid);
2 commentaires
jonas
le 19 Sep 2018
Can you also draw the blocks a,b,c and d in your picture?
I guess this row
%write all the parts into the main array with double size
bou_out=bou_doub(
is where you are stuck, i.e. you have all the pieces but don't know how to compile them?
Réponses (1)
Stephen23
le 21 Sep 2018
Modifié(e) : Stephen23
le 21 Sep 2018
You could do this quite easily with two very simple indices:
>> A = [0,1,2,3;4,5,6,7;8,9,10,11;12,13,14,15]
A =
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
>> S = size(A);
>> H = fix(S./2);
>> R = [H(2):-1:1,1:S(2),S(2):-1:H(2)+1]; % row index
>> C = [H(1):-1:1,1:S(1),S(1):-1:H(1)+1]; % column index
>> B = A(C,R)
B =
5 4 4 5 6 7 7 6
1 0 0 1 2 3 3 2
1 0 0 1 2 3 3 2
5 4 4 5 6 7 7 6
9 8 8 9 10 11 11 10
13 12 12 13 14 15 15 14
13 12 12 13 14 15 15 14
9 8 8 9 10 11 11 10
>> size(B)
ans =
8 8
Note that this is quite efficient as it does not copy any of the data into temporary arrays.
0 commentaires
Voir également
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!