필터 지우기
필터 지우기

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 CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by