필터 지우기
필터 지우기

a function with increasing number of its arguments in a loop

조회 수: 2 (최근 30일)
mehdi J
mehdi J 2018년 10월 27일
편집: Stephen23 2018년 10월 27일
hi dears,
I'm using a function for generating Cartesian products of two or more vectors and I want the number of its arguments increases in each iteration of a loop (the vectors as its arguments are same). For instance in the first iteration of the loop the function calculates the Cartesian(A,A), in the second iteration calculates Cartesian(A,A,A), in the next iteration calculates Cartesian(A,A,A,A), and so on. How could I do it? Thanks
  댓글 수: 2
Kevin Chng
Kevin Chng 2018년 10월 27일
Have you tried out any code? Do you mind to share to us?
mehdi J
mehdi J 2018년 10월 27일
a code has been shared on this link and it is as below. I want to use it in a loop and increase its size in each iteration of the loop
------
function C = cartesian(varargin)
args = varargin;
n = nargin;
[F{1:n}] = ndgrid(args{:});
for i=n:-1:1
G(:,i) = F{i}(:);
end
C = unique(G , 'rows');
end

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

답변 (1개)

Stephen23
Stephen23 2018년 10월 27일
편집: Stephen23 2018년 10월 27일
  댓글 수: 2
mehdi J
mehdi J 2018년 10월 27일
Thanks, but how could I put all of them (all of the cells) in one matrix (below each other)?
Stephen23
Stephen23 2018년 10월 27일
편집: Stephen23 2018년 10월 27일
@Mhedi J: each of the Cartesian products has a different number of rows and columns: this means there is no simple way to concatenate them into one matrix. The simplest solution is to leave them in a cell array, otherwise you would have to pad them with NaN or some other value/s and then concatenate them into one matrix.
Here is a simple example of how to use a cell array:
A = 1:3;
N = 4;
C = cell(1,N);
for k = 1:N;
T = repmat({A},1,1+k);
[T{:}] = ndgrid(T{:});
T = cellfun(@(v)v(:),T,'uni',0);
C{k} = [T{:}];
end

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by