필터 지우기
필터 지우기

How can i generalize what is inside the ndgrid function?

조회 수: 1 (최근 30일)
metehan akinci
metehan akinci 2019년 3월 10일
댓글: metehan akinci 2019년 3월 13일
I have a Values Matrix
Values={[3 4],[2],[3 4 5]} %% which is randomly created.
[Numbers{1:length(Values)}]=ndgrid(Values{1},Values{2},Values{3}); which i want to find.
The problem is that i want to generalize the right sidet so that if Values changes i dont want to re-write my code again.
This is what i tried but couldnt worked it out
for i=1:length(Values)
c(i)={'Values'};
end
Pattern=strjoin(strcat(c,'{%d}'),',');
for a=1:length(Values)
b(a)=a;
end
a=sprintf(Pattern,b) %%%%% which gives me Values{1},Values{2},Values{3}
when i put it in a ndgrid function it doesnt give me the correct answer.
ndgrid(a)
How can i solve this problem? Thanks!
  댓글 수: 2
Stephen23
Stephen23 2019년 3월 11일
편집: Stephen23 2019년 3월 11일
You are already using a comma-separated list on the LHS, just use one on the RHS as well, as Walter Roberson showed:
Numbers = cell(1,numel(Values));
[Numbers{:}] = ndgrid(Values{:})
For more information on comma-separated lists see:

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

채택된 답변

Walter Roberson
Walter Roberson 2019년 3월 10일
편집: Walter Roberson 2019년 3월 10일
nv = length(Values);
Numbers = cell(nv, 1);
[Numbers{:}] = ndgrid(Values{:});

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by