Storing concatenated string array problem

1 回表示 (過去 30 日間)
Matt Guenther
Matt Guenther 2018 年 3 月 15 日
コメント済み: Guillaume 2018 年 3 月 19 日
I have two 1xn matrices that I want to merge together. If array one held [1 PF Pop, 2 PF Pop, 3 PF Pop...] and the other contained [1 AF Pop, 2 AF Pop, 3 AF Pop], then I would like the result to be the concatenation and array of both elements.
% The desired result is shown below, a single matrix concatenated with both elements
|1 AF Pop 1 PF Pop, 1 AF Pop 2 PF Pop,...|
|2 AF Pop 1 PF Pop, 2 AF Pop 2 PF Pop,...|
|3 AF Pop 1 PF Pop, 3 AF Pop 2 PF Pop,...|
% my code so far is below
for P=1:3
F=P*3;
for G = 1:4
G=(G+1)*F;
divbytwo=G/2;
bact8(P,G)=divbytwo; %array of P+1 elements
end
end
bacttry=bact8;
bacttry(1,:)=[];
no_of_rows=size(bacttry);
no_of_columns=no_of_rows;
no_of_columns(:,1)=[];
no_of_rows(:,2)=[];
array_columns=strcat(strtrim(cellstr(num2str((1:no_of_columns)'))'),' PF Pop');
array_rows=strcat(strtrim(cellstr(num2str((1:no_of_rows)'))'),' AF Pop');
  1 件のコメント
Guillaume
Guillaume 2018 年 3 月 19 日
I have no idea which arrays you want to concatenate and into what. However you can create your two array_* much more simply with:
array_columns = compose('%d PF Pop', 1:no_of_columns);
array_rows = compose('%d AF Pop', 1:no_of_rows);
And your way of getting the number of rows and columns is extremely convoluted.
[no_of_rows, no_of_columns] = size(bacttry); %assuming that bacttry is 2D

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

回答 (1 件)

Venkata Siva Krishna Madala
Venkata Siva Krishna Madala 2018 年 3 月 19 日
Hello Matt,
You should consider using cell arrays for this purpose. To concatenate 2 1xn arrays to a single nxn matrix the code will be :-
x1= {'1 PF Pop', '2 PF Pop', '3 PF Pop'}
x2= {'1 AF Pop', '2 AF Pop', '3 AF Pop'}
x={}
for i=1:numel(x1)
for j=1:numel(x2)
x{i,j}=strcat(x1{i},x2{j});
end
end
Regards,
Krishna Madala

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by