フィルターのクリア

Resize array, prevent boundary effect

2 ビュー (過去 30 日間)
Ope
Ope 2018 年 9 月 19 日
編集済み: Stephen23 2018 年 9 月 21 日
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 件のコメント
jonas
jonas 2018 年 9 月 19 日
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 2018 年 9 月 21 日
編集済み: Ope 2018 年 9 月 21 日
thanks Jonas. I tried horzcat for a and b, then vertcat for c and d. I obtained a reasonable result.

サインインしてコメントする。

回答 (1 件)

Stephen23
Stephen23 2018 年 9 月 21 日
編集済み: Stephen23 2018 年 9 月 21 日
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.

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by