matlab - How to create two or more matrices from a single one under certain conditions? -
hello new in matlab , cannot figure out how solve problem.
i have matrix1:
1 0 2 334.456 3 654.7654 4 65.76543 1 0 2 543.43567 3 98.432 4 54.9876 5 12.456
and matrix2:
1 2 2 3 3 4 1 2 2 3 3 4 4 5
matrix2 represents links found in matrix1 in order appear.
i separate links in blocks (matrices) each block starts stop 1. analysing matrix2 should produce 2 new matrices 1 links (1,2)(2,3)(3,4) , other links (1,2)(2,3)(3,4)(4,5). each time find stop 1, starts building new matrix.
a
, b
come out as:
a= [1,2, 334.456; 2,3,654.7654;3,4,65.76543] b=[1,2,543.43567;2,3,98.432;3,4,54.9876;4,5,12.456]
i think want. matrices
cell array containing amount of distinct matrices required (based on amount of 1's in column 1 of matrix2).
matrix1=[1 0; 2 334.456;3 654.7654;4 65.76543;1 0;2 543.43567;3 98.432;4 54.9876;5 12.456]; matrix2=[1 2; 2 3; 3 4; 1 2; 2 3; 3 4; 4 5]; rows=find(matrix2(:,1)==1); % find row numbers 1 in column 1 of matrix 2) rows=[rows(2:end); size(matrix2,1)+1]; % ignore (obvious) first row, add end of matrix2 nrows=size(rows,1); matrices=cell(nrows,1); i=1:nrows lb=1; if i>1 lb=rows(i-1); end matrices{i,1}=zeros(rows(i)-lb,3); j=lb:rows(i)-1 matrices{i,1}(j-lb+1,:)=[matrix2(j,:), matrix1(lb+matrix2(j,2)-2+i,2)]; end end
with following result:
>> matrices{1,1} ans = 1.0000 2.0000 334.4560 2.0000 3.0000 654.7654 3.0000 4.0000 65.7654 >> matrices{2,1} ans = 1.0000 2.0000 543.4357 2.0000 3.0000 98.4320 3.0000 4.0000 54.9876 4.0000 5.0000 12.4560
Comments
Post a Comment