Clear Filters
Clear Filters

Resize array, prevent boundary effect

2 views (last 30 days)
Ope
Ope on 19 Sep 2018
Edited: Stephen23 on 21 Sep 2018
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 Comments
jonas
jonas on 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?
Ope
Ope on 21 Sep 2018
Edited: Ope on 21 Sep 2018
thanks Jonas. I tried horzcat for a and b, then vertcat for c and d. I obtained a reasonable result.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 21 Sep 2018
Edited: Stephen23 on 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.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!