Storing concatenated string array problem

2 次查看(过去 30 天)
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
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 CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by