max and min value of matrix

조회 수: 8 (최근 30일)
Ashvin Hamzah Driwantara
Ashvin Hamzah Driwantara 2017년 1월 6일
답변: Zeeshan Salam 2019년 3월 24일
I have AB matrix that contains 3000x2000 grids data.
AB =
(1,1) (1,2) (1,3) (1,4) ... (1,3000)
(2,1) (2,2) (2,3) (2,4) ... (2,3000)
(3,1) (3,2) (3,3) (3,4) ... (3,3000)
(4,1) (4,2) (4,3) (4,4) ... (4,3000)
(5,1) (5,2) (5,3) (5,4) ... (5,3000)
...
...
...
(2000,1)
I want to calculate (MAX-MIN) value for each 3x5 grids in matrix AB
For Example :
If target grid is (3,2), I use 15 grids data (1~5,1~3) for calculation of MAX and MIN.
And if MAX is (1,1) and MIN is (2,3), then (MAX-MIN) at (3,2) is ((1,1)-(2,3)).
In the same way, if target grid is (3,3), I use (1~5,2~4).
Can you help me to make automatic script to calculate this??
  댓글 수: 2
Stephen23
Stephen23 2017년 1월 6일
@Ashvin Hamzah Driwantara: your question is not clear. Please edit your question and provide us with complete input and output examples so that we can write and test code.
Ashvin Hamzah Driwantara
Ashvin Hamzah Driwantara 2017년 1월 6일
thankyou,, i have edited it

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2017년 1월 6일
편집: Stephen23 2017년 1월 6일
blockproc might do what you want:
>> X = rand(3000,2000);
>> Xmax = blockproc(X,[3,5],@(s)max(s.data(:)));
>> Xmin = blockproc(X,[3,5],@(s)min(s.data(:)));
>> Xdif = Xmax - Xmin;
  댓글 수: 5
Stephen23
Stephen23 2017년 1월 6일
@Ashvin Hamzah Driwantara: have a look at my revised answer. You will need to change the function inside the anonymous function:
@(s)std(s.data(:))
Ashvin Hamzah Driwantara
Ashvin Hamzah Driwantara 2017년 1월 7일
I'm using 5x5 grids, and calculation start from grid (3,3)...
how to calculate grid on (1,1)?

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Guillaume
Guillaume 2017년 1월 6일
I believe Ashvin is looking for nlfilter rather than blockproc. I.e., he wants sliding overlapping blocs instead of adjacent blocks. So:
nlfilter(X, [3 5], @(block) std(block(:)));
  댓글 수: 1
Ashvin Hamzah Driwantara
Ashvin Hamzah Driwantara 2017년 1월 6일
편집: Ashvin Hamzah Driwantara 2017년 1월 6일
it works,, but it takes a long time...
how about using colfilt?
can you help me, sir?

댓글을 달려면 로그인하십시오.


Ashvin Hamzah Driwantara
Ashvin Hamzah Driwantara 2017년 1월 6일
I get an error
BLOCKPROC encountered an error while evaluating the user-supplied function handle, FUN.
The cause of the error was:
Undefined function 'min' for input arguments of type 'struct'.
Error in blockprocFunDispatcher (line 13)
output_block = fun(block_struct);
Error in blockprocInMemory (line 80)
[ul_output fun_nargout] = blockprocFunDispatcher(fun,block_struct,...
Error in blockproc (line 242)
result_image = blockprocInMemory(source,fun,options);
please help
  댓글 수: 6
Stephen23
Stephen23 2017년 1월 6일
편집: Stephen23 2017년 1월 7일
@Ashvin Hamzah Driwantara: your explanation is not clear. If each block does not generate one value, then you will need to explain in more detail, and provide input and output sample matrices so that we can test code. Until you provide a clear explanation of what you want there is nothing more that I can do for you.
Ashvin Hamzah Driwantara
Ashvin Hamzah Driwantara 2017년 1월 6일
yes, I'm sorry... the matrix is from nc file...
do you know how to solve the problem using colfilt?
because nlfilter takes a long time

댓글을 달려면 로그인하십시오.


Ashvin Hamzah Driwantara
Ashvin Hamzah Driwantara 2017년 1월 7일
I have solved my problem using this script
c13 = matrix 3000x2000
matrix_WV=c13;
[tinggi,lebar]=size(matrix_WV);
maxmin=zeros(tinggi,lebar);
for i=1:tinggi
for j=1:lebar
%fprintf('hitung grid ke (%d,%d)\n',i,j);
n=0;
for tempi=-2:2
for tempj=-2:2
if i+tempi>=1 && i+tempi<=tinggi && j+tempj>=1 && j+tempj<=lebar
n=n+1;
area5x5(n)=matrix_WV(i+tempi,j+tempj);
end
end
end
max=area5x5(1);
min=area5x5(1);
for k=1:n
if max<area5x5(k)
max=area5x5(k);
end
if min>area5x5(k)
min=area5x5(k);
end
end
maxmin(i,j)=max-min;
end
end
%fprintf('done\n');

Zeeshan Salam
Zeeshan Salam 2019년 3월 24일
i=3:3:smax what is meaning of this?

Community Treasure Hunt

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

Start Hunting!

Translated by