Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, let's declare the <em>symbolics</em> easier using <a href="http://www.mathworks.com/help/symbolic/sym.html" rel="nofollow"><code>sym</code></a>:</p> <pre><code>T = sym('T%d%d', [4 4]); A = sym('A%d%d', [4 4]); L = sym('L', [4 1]); </code></pre> <p>There are several problems with the original code; <strong>1.</strong> <code>f</code> is being replaced in each inner iteration. <strong>2.</strong> The inner result should be scalar and thus <code>I</code> must not appear there. (Note that you can also define <code>I</code> like <a href="http://www.mathworks.com/help/matlab/ref/eye.html" rel="nofollow"><code>eye(4)</code></a> instead of writing it manually.)</p> <p>Here is the corrected version:</p> <pre><code>f = cell(4,1); % Initialize equation system for r = 1:k for k = 1:4 for i = 1:4 f{r}(i,k) = T(i,k) * A(r,k); end end f{r} = f{r} - L(r)*diag(A(r,:)); end </code></pre> <p><code>f{i}</code> would be the <code>ith</code> slice.</p> <p><strong>Note</strong>: As @Schorsch pointed out (and Matlab also shows a <em>warning</em>) always try to use another variable name other than <code>i</code> (or <code>j</code>), since they represent the <a href="http://www.mathworks.com/help/matlab/ref/i.html" rel="nofollow"><em>imaginary unit</em></a>.</p> <p>Just for fun you can use <a href="http://www.mathworks.com/help/matlab/ref/repmat.html" rel="nofollow"><code>repmat</code></a> to remove the two inner loops:</p> <pre><code>for r = 1:4 f{r} = T .* repmat(A(r,:), [4 1]) - L(r)*diag(A(r,:)); end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload