calculating a few elements of a table, using a different equation
1 次查看(过去 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!
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!