简体   繁体   English

无法在 GNU Octave 中向矩阵添加列

[英]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.我正在写一个 function,truthtable.m,我可以在其中输入一个包含一系列二进制输入的矩阵,然后输出一个 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.我有一个工作 function,binvargen.m,它为给定位数的每个可能输入生成一个二进制字符串,并将其输入到程序中。

I cannot seem to create a new matrix which contains both the inputs and the output.我似乎无法创建一个包含输入和 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.我尝试在输入矩阵上使用 reshape,认为我可以将输出作为额外的列添加到它以形成我的 output,但它会抛出一个没有解释的错误,只是说它无法调整矩阵的大小。

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.然而,当我这样做时,它只是打印输入矩阵并完全忽略 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: output 是:

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

I would like for output(counttest,1:variables) to be the same as input.我希望 output(counttest,1:variables) 与输入相同。 In other words, the output should be input, but with an extra column added at the end.换句话说,应该输入 output,但在末尾添加了一个额外的列。 The output should look like this: output 应如下所示:

[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.我仍然不太确定我之前做错了什么,但是累了确实会导致愚蠢的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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