필터 지우기
필터 지우기

how to generalize a nested for-loop?2

조회 수: 1 (최근 30일)
Mnr
Mnr 2015년 11월 2일
편집: Image Analyst 2015년 11월 4일
Hello all,
Is there a way to generalize that nested loop so that it works for any sizes of h1? Thank you.

답변 (2개)

Walter Roberson
Walter Roberson 2015년 11월 2일
coeffs = [0.3, 0.2, 0.5, 0.8, 0.1, 0.75]; %for example
V = [1+1i +1-1i -1+1i -1-1i];
n = length(coeffs);
[Vg{1:n}] = ndgrid(V);
Vm = cell2mat( cellfun(@(C) C(:), Vg, 'Uniform', 0) );
t_h1 = Vm * coeffs(:);
h1 = reshape(t_h1, length(V) * ones(1,n));
clear n Vg Vm t_h1
  댓글 수: 1
Mnr
Mnr 2015년 11월 2일
편집: Image Analyst 2015년 11월 4일
It works for any sizes of coeffs! Thank you!

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


Stephen23
Stephen23 2015년 11월 2일
@Mnr: try this:
V = [1+1i,+1-1i,-1+1i,-1-1i];
W = [0.3,0.2,0.5,0.8];
N = numel(V);
X = cell(1,N);
[X{:}] = ndgrid(V);
Y = cat(N+1,X{:});
S = [ones(1,N),N];
Z = sum(bsxfun(@times,reshape(W,S),Y),N+1);
  댓글 수: 4
Mnr
Mnr 2015년 11월 2일
I checked again and deleted my comment few minutes after I typed it. I do not know why it is still shown to you. Thank you, they are equal and your method is fantastic. Is there a way to accept both the two answers?
Stephen23
Stephen23 2015년 11월 2일
I believe only one answer can be accepted.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by