2

Let us suppose that we have two matrices as input, X and Y. I would like to regress each column of Y on each column of X and calculate several parameters, then create a table for the results. Here is my starting code:

function [Table]=create_table(Y,X)
    [n,p]=size(X); % size of both matrix is X
    for ii=1:p % iterate  over all variable 
        x=X(:,i);
        y=Y(:,ii);
        x = [ones(size(x)) x];% construct  X matrix 
        [b,~,~,~,~] = regress(y,x);

        %% let us suppose we would like to calculate two parameters
        unknown=b(1)*100-b(2);
        known=b(2)/b(1)+200
    end
end

What I want to get as a result is following table (let us suppose that p = 3):

enter image description here

I know there is a table command in MATLAB, but I don't know how to use it here?

gnovice
  • 125,304
  • 15
  • 256
  • 359

1 Answers1

2

Immediately before your for loop, you can initialize your table (filled with zeroes to start) like so:

T = table((1:p).', zeros(p, 1), zeros(p, 1), ...
          'VariableNames', {'Iteration', 'Unknown', 'Known'});

Then you can fill in rows of your table within your loop by replacing the calculations of unknown and known with this:

T.Unknown(ii) = b(1)*100-b(2);
T.Known(ii) = b(2)/b(1)+200;
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • after cycle should i fill table? –  Sep 23 '17 at 18:07
  • @datodatuashvili: I don't know what you mean. I think I made it clear where the lines above have to go in your code? – gnovice Sep 23 '17 at 18:11
  • i will try and i will post if something i didnot undertsnad –  Sep 23 '17 at 18:12
  • what does this code means?Error using optimal_estimation (line 44) To assign to or create a variable in a table, the number of rows must match the height of the table –  Sep 23 '17 at 18:23
  • @datodatuashvili: Make sure that when you are accessing a variable in the table you are typing the name *exactly* as it appears in the table (case matters). – gnovice Sep 23 '17 at 18:37