working with an array pointer in a recursive function

7 ビュー (過去 30 日間)
Itzik Ben Shabat
Itzik Ben Shabat 2012 年 11 月 8 日
H, I want to create a recursive function that gets an image matrix and turns it into a quadtree array. so i figured the best way to do this is with a recursive function. so this is what i came up with:
function [void] = ConstrucQuadtree( A,n,QuadtreeArray,index )
%ConstrucQuadtree constructs a quadtree array from a greyscale image matrix
%A recursively
if (n=1)
QuadtreeArray(i)=A;
index=index+1;
else
ConstrucQuadtree(A(0:n/2,0:n/2),length(A(0:n/2,0:n/2),QuadtreeArray,index);
ConstrucQuadtree(A(0:n/2,n/2:),length(A(0:n/2,n/2:n),QuadtreeArray,index);
ConstrucQuadtree(A(n/2:n,0:n/2),length(A(n/2:n,0:n/2),QuadtreeArray,index);
ConstrucQuadtree(A(n/2:n,n/2:n),length(A(n/2:n,n/2:n),QuadtreeArray,index);
end
end
since i never worked with recursive functions in MATLAB i have a few questions : 1. how do i return void ? (i guess what i wrote in my code is illegal) 2. how do i work with pointers? (both index and QuadtreeArray are supposed to change from one recursion to another.
thanks

採用された回答

Daniel Shub
Daniel Shub 2012 年 11 月 8 日
If you want to return "void" you simply do not return anything ...
function ConstrucQuadtree( A,n,QuadtreeArray,index )
MATLAB is lazy and passes pointers around when it can and only allocates new memory when necessary. For your purposes if instead you return QuadtreeArray and index, MATLAB will not allocate new memory. It i important that you return a variable of the same name as the input ...
function [QuadtreeArray, index] = ConstrucQuadtree( A,n,QuadtreeArray,index )
and then modify your code to accept the returned arguments. Something like ...
[QuadtreeArray, index] = ConstrucQuadtree(A(0:n/2,0:n/2),length(A(0:n/2,0:n/2),QuadtreeArray,index);
  1 件のコメント
Itzik Ben Shabat
Itzik Ben Shabat 2012 年 11 月 8 日
thanks! worked great! (aside from a small indexing problem, forgot that the array starts from 1 and not 0... ) :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by