等长两个向量的逐元素比较

时间:2019-05-22 14:58:53

标签: r match

我希望使用R在具有相同长度的两个字符向量之间比较每个元素。我可以使用for循环轻松地做到这一点,但是我正在寻找一种替代方法。例如,

x<-c("A", "B", "C", "D", "E", "C")
y<-c("A", "B", "E", "D", "E", "C")

matches<-vector(length=6)

for(i in 1:6){
   matches[i]<-match(x[i], y[i])
}

matches

[1]  1  1 NA  1  1  1

这似乎很简单,但是我很难弄清楚。

谢谢

1 个答案:

答案 0 :(得分:1)

我们可以做library IEEE; use IEEE.STD_LOGIC_1164.ALL; use work.pkg.all; entity porting is -- Port ( ); end porting; architecture Behavioral of porting is component main is port ( rstb: in std_logic; clk: in std_logic; en: in std_logic; row: out integer; col: out integer; last_elem: out integer; output_Data:out matrixData ); end component; component shift_reg is port( clk:in std_logic; reset:in std_logic; input:in integer; q:out int_vector; iter:out integer ); end component; signal data:matrixData; signal row1: integer; signal col1:integer; signal element:integer; signal clk:std_logic:='0'; signal enable:std_logic:='0'; signal reset:std_logic:='0'; signal input_elem:integer; signal output_data:int_vector; signal iteration: integer; constant clk_period: time:=10 ns; begin p1: main port map('0','1',enable,row1,col1,element,data); p2: shift_reg port map(clk,'0',input_elem,output_data,iteration); clk_process :process variable state:integer:=0; variable r,c:integer; begin state:=state+1; c:=state mod col1; r:=state/col1; input_elem<=data(r,c); clk <= '0'; wait for clk_period/2; clk <= '1'; wait for clk_period/2; end process; enable_process:process begin enable<='1'; wait for clk_period*52538; enable<='0'; end process; end Behavioral; 来逐元素比较两个向量

==

对于匹配值,它返回x == y ,对于不匹配,它返回TRUE

如果我们需要转换为FALSE1

NA