calculating a few elements of a table, using a different equation

조회 수: 2 (최근 30일)
Hello!
Let's say that i am calculating every element of a 100X300 table by using equation A I use two for loops for i=1:100 for j=1:300 element(i,j)=... (it is the equation A) end end equation A involves the neighboring elements What if, a few elements of the table must be calculated with a different equation, equation B and this must happen inside the two for loops
for example elements (49,81) (49,82) (50,81) (50,82) (51,81) (51,82) (52,81) (52,82) i could use if and elseif function for every single element, like if i==49 && j==81 element(i,j)=...equation B elseif i==50 && j==81 element(i,j)=...equation B ...
...
end Is there another, better way to do it ???
Thanks a lot!

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 5월 31일
a = 49:52
b = 81:82
ii = fullfact([numel(b) numel(a)])
for jj = 1:size(ii,1)
element(a(ii(jj,2)),b(ii(jj,1))) = ...equation;
end
or
[b1,a1] = ndgrid(b,a);
ii = a1(:);
jj = b1(:);
for ij = 1:numel(ii)
element(ii(ij),jj(ij)) = ...equation;
end
  댓글 수: 2
vaggelis vaggelakis
vaggelis vaggelakis 2012년 6월 1일
Thanks Andrei!
But i want this to be executed only when i and j indices are pointing to the listed elements,(49,81) (49,82) (50,81) (50,82) (51,81) (51,82) (52,81) (52,82)
the table i calculate has 101X301 elements and is named T. So
for i=1:101
for j=1:301
T(i,j)=...equation A
end
end
when i=49 and j=81 i want to calculate this point with equation B inside these two "for" loops!
I think, what you sent me is going to be executed on every iteration in these loops while i want equation B being executed ONLY when i,j are referring to these few predefined elements
I already tried to use "ismember" function, it worked but my code was significantly slower!
I also tried "if-ifelse" function for every each one of these elements(equation B) following by "else" for all the others (equation A). It works fine, but i m looking for a better code!
vaggelis vaggelakis
vaggelis vaggelakis 2012년 6월 1일
I forgot to mention that all the calculations are implicating the neighboring elements, that's why i am calculating element by element and the "equation A" elements can't be calculated without the "equation B" inside the loops!

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

추가 답변 (0개)

카테고리

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