开发者

matrix dimensions matlab

开发者 https://www.devze.com 2023-04-05 02:11 出处:网络
I Have my function below, the idea being that X is a 3x3 extract from T to be used in the loop, it correctly extracts the 3 rows but for some reason produces far too many columns, see example below.

I Have my function below, the idea being that X is a 3x3 extract from T to be used in the loop, it correctly extracts the 3 rows but for some reason produces far too many columns, see example below.

function T = tempsim(rows, cols, topNsideTemp, bottomTemp, tol)
    T = zeros(rows,cols);
    T(1,:) = topNsideTemp;
    T(:,1) = topNsideTemp;
    T(:,rows) = topNsideTemp;
    T(rows,:) = bottomTemp;
    S = [0 1 0; 1 1 1; 0 1 0]; 
    X = zeros(3,3);
    A = zeros(3,3);
    for ii = 2:(cols-1);
        jj = 2:(rows-1);
       开发者_高级运维 X = T([(ii-1) ii (ii+1)], [(jj-1) jj (jj+1)])
        A = X.*S;
        T = (sum(sum(A)))/5 
    end

test sample

EDU>> T = tempsim(5,4,100,50,0)

X =

   100   100   100   100   100   100   100   100   100
   100     0     0     0     0     0     0     0   100
   100     0     0     0     0     0     0     0   100


ans =

   100   100   100   100   100   100   100   100   100
   100     0     0     0     0     0     0     0   100
   100     0     0     0     0     0     0     0   100

??? Error using ==> times
Matrix dimensions must agree.

Error in ==> tempsim at 14
    A = X.*S;

any thoughts on how to fix this?


There's no need to preallocate X and A if you do a complete assignment anyway. Then, you replace T with a scalar inside the loop, which makes you run into problems in the next iteration. What I'm guessing you want could look something like this:

function T = tempsim(rows, cols, topNsideTemp, bottomTemp, tol)
  T = zeros(rows,cols);
  T(1,:) = topNsideTemp;
  T(:,1) = topNsideTemp;
  T(:,rows) = topNsideTemp;
  T(rows,:) = bottomTemp;
  S = [0 1 0; 1 1 1; 0 1 0]; 
  for ii = 1:(cols-2);
    for jj = 1:(rows-2);
      X = T(ii:ii+2, jj:jj+2);
      A = X.*S;
      T(ii,jj) = (sum(sum(A)))/5;
    end
  end

Although I'm not sure if you really mean to do that – you're working on T while modifying it. As a wild guess, I suspect you might be looking for something like

conv2(T, S/5, 'same')

instead, perhaps after making your fixed-temp borders twice as thick and re-setting them after the call (since conv2 does zero-padding at the outer borders).


Here:

jj = 2:(rows-1);
X = T([(ii-1) ii (ii+1)], [(jj-1) jj (jj+1)])

jj becomes [2 3 4]

so X is

T([1 2 3], [ [2 3 4]-1 [2 3 4] [2 3 4]+1 ])

You probably missed a for loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号