最近学习了一些简单的MATLAB技巧,包括去除任意位置的向量元素、对向量元素进行循环、连接字符串、分割字符串。由于不同编程语言语法容易混淆,因此顺便对比一下这些功能如何在R里实现。(1) 去除向量任意位置的元素MATLAB:A = [1, 3, 5];

%% define a vector named AA(2) = [];

%% remove the second element of AR:A <- c(1, 3, 5);

## define a vector named AA <- A[-2]

## remove the second element of A(2) 对向量元素进行循环MATLAB:for i = A    %% some codes to deal with each element of vector AendR:for (i in A){    ## some codes to deal with each element of vector A}(3) 连接字符串MATLAB:X = 'abc'; Y = '123';

%% define two string vector X and YZ = [X,Y];

%% concatenate two strings and assign to Z, whose content is 'abc123'Z = strcat(X, Y);

%% another way for concatenating two stringsR:X <- 'abc'; Y <- '123'Z <- paste(X, Y, sep='')

## the parameter 'sep' spcify the delimiter when concatenating two strings(4) 分割字符串MATLAB:P = '/home/user/alex';

%% a string contains a pathV = strsplit(P, '/');

%% split the string P into different parts delimited by '/', and save the results into a cell array VP = strjoin(V, '/');

%% convert back into a path after some operationsR:P <- '/home/user/alex'V <- unlist(strsplit(P, '/'))

## the 'strsplit' would result in a list variable typeP <- paste(V, '/', sep='', collapse='')

## convert back into a path after some operations参考:(1) -and-arrays.html(2) https://stackoverflow.com/questions//how-to-iterate-over-a-column-vector-in-matlab(3) https://stackoverflow.com/questions/16401244/matlab-how-to-concatenate-strings(4) https://www.mathworks.com/help/matlab/ref/strsplit.html(5) https://www.mathworks.com/matlabcentral/answers/36578-convert-cell-to-string-or-assign-variable(6) https://stackoverflow.com/questions/2098368/concatenate-a-vector-of-strings-character