MATLAB Answers

Thomas
0

Matrix build: Switch/Case and For loop

Asked by Thomas
on 24 Feb 2013
I am attempting to code a 32X32 matrix and a 32X1 matrix using a switch inside of a for loop. I keep getting an error stating
"Undefined function 'eq' for input arguments of type 'cell'."
When running the below stated code. Can anyone please point to what I am doing wrong? Thank you.
A = zeros(32,32);
C = zeros(32,1);
for i = 1:32;
switch i
case i == 1
A(i,1) = -4;
A(i,2) = 1;
A(i,5) = 1;
C(i) = -600;
case i == {2,3}
A(i,i-1) = 1;
A(i,i+1) = 1;
A(i,i+4) = 1;
A(i,i) = -4;
C(i) = -300;
case i == 4
A(i,3) = 2;
A(i,4) = -4;
A(i,8) = 1;
C(i) = -300;
case i == 29
A(i,25) = 2;
A(i,29) = -6;
A(i,30) = 1;
C(i) = -500;
case i == 32
A(i,28) = 2;
A(i,31) = 2;
A(i,32) = -6;
C(i) = -200;
case i == {5,9,13,17,21,25}
A(i,i+1) = 1;
A(i,i-4) = 1;
A(i,i+4) = 1;
A(i,i) = -4;
C(i) = -300;
case i == {6,7,10,11,14,15,18,19,22,23,26,27}
A(i,i-1) = 1;
A(i,i+1) = 1;
A(i,i-4) = 1;
A(i,i+4) = 1;
A(i,i) = -4;
C(i) = 0;
case i == {8,12,16,20,24,28}
A(i,i-1) = 2;
A(i,i-4) = 1;
A(i,i+4) = 1;
A(i,i) = -4;
C(i) = 0;
case i == {30,31}
A(i,i-1) = 1;
A(i,i+1) = 1;
A(i,i-4) = 2;
A(i,i) = -6;
C(i) = -200;
end
end

  0 Comments

Sign in to comment.

1 Answer

Answer by Azzi Abdelmalek
on 24 Feb 2013
Edited by Azzi Abdelmalek
on 24 Feb 2013

Just write case {2,3}
i=2
switch i
case 1
A(i,1) = -4;
A(i,2) = 1;
A(i,5) = 1;
C(i) = -600;
case {2,3}
A(i,i-1) = 1;
A(i,i+1) = 1;
A(i,i+4) = 1;
A(i,i) = -4;
C(i) = -300;
end

  1 Comment

Thank you very much, fixed the problem

Sign in to comment.