Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在matlab中查找行和列_Matlab_Matrix_Find - Fatal编程技术网

如何在matlab中查找行和列

如何在matlab中查找行和列,matlab,matrix,find,Matlab,Matrix,Find,我有一个变量矩阵: A = [1 2 8 8 1 4 6 8 1 1 5 3 1 1 8]; 我有变量B: B=[2 3 1 8 8]; 问题是如何从变量B中找到变量A中的行和列(按行排序) 例如,变量B中的第一个索引是2,然后我想在变量A中找到值2,并得到第一行和第一列,下一个过程直到索引5,但如果已经使用了行和列,则得到第二个位置(例如,索引4和5具有相同的值) 结果是:

我有一个变量矩阵:

A = [1     2     8     8     1
     4     6     8     1     1
     5     3     1     1     8];
我有变量B:

B=[2 3 1 8 8];
问题是如何从变量B中找到变量A中的行和列(按行排序)

例如,变量B中的第一个索引是2,然后我想在变量A中找到值2,并得到第一行和第一列,下一个过程直到索引5,但如果已经使用了行和列,则得到第二个位置(例如,索引4和5具有相同的值)

结果是:

rows = 1 3 1 1 1
columns = 2 2 1 3 4

使用
查找
。该函数可以返回线性索引或行/列索引

使用线性指数可以得到一个解决方案

idx = zeros(size(B));
for i = 1:numel(B)
    % Find all indexes
    tmpIdx = find(A == B(i));
    % Remove those already used
    tmpIdx = setdiff(tmpIdx, idx);
    % Get the first new unique
    idx(i) = tmpIdx(1);
end
% Convert index to row and col
[rows, cols] = ind2sub(size(A),idx)
给予:

rows =  1 3 1 1 2
cols =  2 2 1 3 3
请注意,随着线性索引逐列向下移动,这里的结果与示例中的结果不同(尽管仍然是正确的索引)


但要实现这一点,您只需转置A矩阵(
A.
)并翻转行和列(来自
ind2sub
)的结果)

使用
find
。该函数可以返回线性索引或行/列索引

使用线性指数可以得到一个解决方案

idx = zeros(size(B));
for i = 1:numel(B)
    % Find all indexes
    tmpIdx = find(A == B(i));
    % Remove those already used
    tmpIdx = setdiff(tmpIdx, idx);
    % Get the first new unique
    idx(i) = tmpIdx(1);
end
% Convert index to row and col
[rows, cols] = ind2sub(size(A),idx)
给予:

rows =  1 3 1 1 2
cols =  2 2 1 3 3
请注意,随着线性索引逐列向下移动,这里的结果与示例中的结果不同(尽管仍然是正确的索引)


但是为了得到这个结果,你可以转置A矩阵(
A.
)并翻转行和列(来自
ind2sub的结果)

这里是我使用for循环的解决方案,我试图优化迭代次数和计算成本。如果B和A之间没有对应的值,则行/列索引返回NaN

[Bu,~,ord] = unique(B,'stable');

% Index of each different values
[col,row] = arrayfun(@(x) find(A'==x),Bu,'UniformOutput',0)

% For each value in vector B we search the first "non already used" corresponding value in A.
for i = 1:length(B)
    if ~isempty(row{ord(i)})
        r(i) = row{ord(i)}(1);
        row{ord(i)}(1) = [];
        c(i) = col{ord(i)}(1);
        col{ord(i)}(1) = [];
    else
        r(i) = NaN;
        c(i) = NaN;
    end
end
结果:

c = [2   2   1   3   4]

r = [1   3   1   1   1]

这是我使用for循环的解决方案,我试图优化迭代次数和计算成本。如果B和A之间没有对应的值,则行/列索引返回NaN

[Bu,~,ord] = unique(B,'stable');

% Index of each different values
[col,row] = arrayfun(@(x) find(A'==x),Bu,'UniformOutput',0)

% For each value in vector B we search the first "non already used" corresponding value in A.
for i = 1:length(B)
    if ~isempty(row{ord(i)})
        r(i) = row{ord(i)}(1);
        row{ord(i)}(1) = [];
        c(i) = col{ord(i)}(1);
        col{ord(i)}(1) = [];
    else
        r(i) = NaN;
        c(i) = NaN;
    end
end
结果:

c = [2   2   1   3   4]

r = [1   3   1   1   1]

使用可以使用find和sub2ind实现您想要的功能 但对于这一点,你必须先把你的A转换成A

 A = [1     2     8     8     1
     4     6     8     1     1
     5     3     1     1     8];
B= [2 3 1 8 8];
TMP = A.';
for i = 1:length(B)
   indx = find(TMP== B(i),1,'first')   %Finding the element of B present in A 
   if(~isempty(indx )) % If B(i)  is a member of A 
       [column(i),row(i)] = ind2sub(size(TMP),indx) % store it in row and column matrix
       TMP(indx) = nan; % remove that element
   end
end
纵队=

2     2     1     3     4
划船=

1     3     1     1     1
正如在一篇评论中,Usama建议预先分配内存 你可以通过使用

row = zeros(1,sum(ismember(B,A)))
column= zeros(1,sum(ismember(B,A)))

即使A中不存在B的某些成员,上述代码仍然有效。Use可以使用find和sub2ind来实现您想要的功能 但对于这一点,你必须先把你的A转换成A

 A = [1     2     8     8     1
     4     6     8     1     1
     5     3     1     1     8];
B= [2 3 1 8 8];
TMP = A.';
for i = 1:length(B)
   indx = find(TMP== B(i),1,'first')   %Finding the element of B present in A 
   if(~isempty(indx )) % If B(i)  is a member of A 
       [column(i),row(i)] = ind2sub(size(TMP),indx) % store it in row and column matrix
       TMP(indx) = nan; % remove that element
   end
end
纵队=

2     2     1     3     4
划船=

1     3     1     1     1
正如在一篇评论中,Usama建议预先分配内存 你可以通过使用

row = zeros(1,sum(ismember(B,A)))
column= zeros(1,sum(ismember(B,A)))


即使A中不存在B的某些成员,上述代码仍然有效。

@Sardar_Usama啊,是的,正确。但对于非复杂矩阵/数组,它们是相同的,对吗?或者这是我不知道的另一个区别?这是因为实数的共轭是同一个数!因此,您可能会说功能是相同的,但是如果您说
A'
是转置,那么这个语句是不正确的@Sardar_Usama啊,是的,没错。但对于非复杂矩阵/数组,它们是相同的,对吗?或者这是我不知道的另一个区别?这是因为实数的共轭是同一个数!因此,您可能会说功能是相同的,但是如果您说
A'
是转置,那么这个语句是不正确的!考虑预先分配内存为<代码> r>代码>和<代码> c>代码>,因为它不被认为是在循环中增长向量大小的一个好的编程实践。作为参考,请阅读以下内容:您的解决方案似乎有效,但:-)@Sardar_Usama不可能预先分配
col
row
,因为输出不一致。但是可以创建
c=0(长度(B),1)
考虑为
r
c
预先分配内存,因为在循环中增加向量大小被认为不是一种好的编程实践。作为参考,请阅读以下内容:您的解决方案似乎有效,但:-)@Sardar_Usama不可能预先分配
col
row
,因为输出不一致。但是可以创建
c=0(长度(B),1)此解决方案的一个缺点是您更改了矩阵A。您可以将其存储在一些临时矩阵中。我不知道它是否对OP有用,但如果B的一个值大于A的最大值,您的代码将crash@obchardon不,不会的!考虑预先分配内存用于<代码>行<代码> >代码>列>代码>,因为它不被认为是在循环中增长向量大小的一个好的编程实践。作为参考,请阅读以下内容:此解决方案的一个缺点是您更改了矩阵A。您可以将其存储在一些临时矩阵中。我不知道它是否对OP有用,但如果B的一个值大于A的最大值,您的代码将crash@obchardon不,不会的!考虑预先分配内存用于<代码>行<代码> >代码>列>代码>,因为它不被认为是在循环中增长向量大小的一个好的编程实践。请阅读以下内容以供参考: