merging two cell type matrix

1 回表示 (過去 30 日間)
sermet
sermet 2016 年 4 月 21 日
コメント済み: Andrei Bobrov 2016 年 4 月 21 日
start_id=[{'p1'};{'p2'};{'p1'};{'p1'};{'p3'}]; %cell
finish_id=[{'p2'};{'p3'};{'p3'};{'p4'};{'p4'}]; %cell
points_id=[{'p1'};{'p2'};{'p3'};{'p4'}]; %cell
m=numel(points_id);
for i=1:m
IndexC_minus(:,i) = strfind(start_id, points_id{i});
IndexC_positive(:,i) = strfind(finish_id, points_id{i});
end
I need to create IndexC_minus_double and IndexC_positive_double matrix as follows;
IndexC_minus_double=[-1 0 0 0;0 -1 0 0;-1 0 0 0;-1 0 0 0;0 0 -1 0]; %for each [1] cell, write -1 numeric and for each [] cell write 0 numeric.
IndexC_positive_double=[0 1 0 0;0 0 1 0;0 0 1 0;0 0 0 1;0 0 0 1]; %for each [1] cell, write 1 numeric and for each [] cell write 0 numeric.
Finally, I need to merge these 2 matrix as below;
merged_matrix=[-1 1 0 0;0 -1 1 0;-1 0 1 0;-1 0 0 1;0 0 -1 1]; % overlapped 0 produces 0 in the merged_matrix. The location of 1 and -1 in the IndexC_minus_double and IndexC_positive_double matrix are the same in the merged_matrix.
  1 件のコメント
Guillaume
Guillaume 2016 年 4 月 21 日
Did you really mean to use strfind (which will find p1 in abcp1d) and not just compare the strings for equality with strcmp?
You have an odd way of constructing cell arrays. This is simpler:
start_id = {'p1'; 'p2'; 'p1'; 'p1'; 'p3'};

サインインしてコメントする。

採用された回答

Andrei Bobrov
Andrei Bobrov 2016 年 4 月 21 日
編集済み: Andrei Bobrov 2016 年 4 月 21 日
merged_matrix = bsxfun(@strcmp,finish_id,points_id')...
- bsxfun(@strcmp,start_id,points_id');
ADD
[~,~,c] = unique([start_id;finish_id;points_id]);
ii = zeros(numel(c),1);
n = cellfun(@numel,{start_id;finish_id;points_id});
ii(cumsum(n) - n + 1) = 1;
ii = cumsum(ii);
c3 = c(ii == 3)';
merged_matrix = bsxfun(@eq,c(ii==2),c3) - bsxfun(@eq,c(ii==1),c3)
in your case
v = {start_id;finish_id;points_id};
for ii = 1:3
a = regexp(v{ii},'\d','match');
v{ii} = str2double(cat(1,a{:}));
end
merged_matrix = bsxfun(@eq,v{2},v{3}') - bsxfun(@eq,v{1},v{3}');
other variant
n = numel(points_id);
s = repmat(start_id,1,n);
f = repmat(finish_id,1,n);
nn = repmat(points_id',numel(finish_id),1);
merged_matrix = strcmp(f,nn) - strcmp(s,nn);
  5 件のコメント
sermet
sermet 2016 年 4 月 21 日
編集済み: sermet 2016 年 4 月 21 日
Andrei, it gives,
Error using -
Matrix dimensions must agree
merged_matrix = bsxfun(@eq,c(ii==2),c3) - bsxfun(@eq,c(ii==1),c3)
from codes,
bsxfun(@eq,c(ii==2),c3)= []
bsxfun(@eq,c(ii==1),c3)=Empty matrix: 3-by-0
sermet
sermet 2016 年 4 月 21 日
thank you very much, Andrei.

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2016 年 4 月 21 日
This is probably much simpler than Andrei's answer. I assume you meant to use strcmp in your original example:
IndexC_minus_double = cell2mat(cellfun(@(s) -strcmp(s, points_id)', start_id, 'UniformOutput', 0))
IndexC_positive = cell2mat(cellfun(@(s) +strcmp(s, points_id)', finish_id, 'UniformOutput', 0))
merged_matrix = IndexC_minus_double + IndexC_positive
Note that I assume that -1 and +1 don't overlap for the merge matrix since you haven't said what happens if this is the case
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2016 年 4 月 21 日
+1

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by