简体   繁体   中英

Unable to Add Column to Matrix in GNU Octave

I am writing a function, truthtable.m, into which I can input a matrix containing a series of binary inputs, and which then outputs a truth table for a function. I have a working function, binvargen.m, that generates a binary string for each possible input for a given number of bits, and it feeds that into the program.

I cannot seem to create a new matrix which contains both the inputs and the output. I've tried using reshape on the input matrix, thinking that I can add my outputs as an extra column to it to form my output, but it throws an error with no explanation, simply saying that it can't resize the matrix.

I've also tried creating a new matrix from scratch, and feeding the input matrix into it and then feeding the outputs into it. When I do this however, it simply prints the input matrix and totally ignores the output. I have no idea what I'm doing wrong.

input = [000;
001;
010;
011;
100;
101;
110;
111;]

This is my current code, with the offending bits cut out:

function output = truthtable(input)
  tests = size(input,1);
  variables = size(input,2);
  for counttest = 1:tests,
    for countvars = 1:variables,
      out(countvars) = str2num(input(counttest,countvars));
    endfor
    output(counttest,variables+1) = f1a(out(:));
  endfor

The output for this is:

[0, 0, 0, 0, 1, 1, 1, 1]

I would like for output(counttest,1:variables) to be the same as input. In other words, the output should be input, but with an extra column added at the end. The output should look like this:

[0000;
0010;
0100;
0110;
1001;
1011;
1101;
1111;]

Help is appreciated.

After much frustration, I went to bed, slept on it, and played around with it today with a well rested mind and coffee. Beaker's answer didn't do quite what I needed, but it did point me in the right direction. What I have ended with is this:

function output = truthtable(input)
  tests = size(input,1);
  variables = size(input,2);
  for counttest = 1:tests,
    for countvars = 1:variables,
      out(countvars) = str2num(input(counttest,countvars));
    endfor
    output(counttest,:) = strcat(input(counttest,:),"=",num2str(f1a(out(:))));
  endfor

I'm still not quite sure what I was doing wrong before, but being tired does lead to stupid mistakes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM