Split a double array by zero and store in a column matrix

조회 수: 1 (최근 30일)
This is my code. But it gives the error 'Dimensions of matrices being concatenated are not consistent'.
My expected output is;
2 5 6 8 9
11 13 14 18
3 7 15 19 20
Please help!
Routes=[0 2 5 6 8 9 0 11 13 14 18 0 3 7 15 19 20 0];
Display=[];
Current_Cluster=[];
for i=2:length(Routes)
if Routes(i) ~=0
Current_Cluster=[Current_Cluster Routes(i)];
else
Display=[Display ; Current_Cluster];
Current_Cluster=[];
end
end
disp('Çulsters are');
Display

채택된 답변

Walter Roberson
Walter Roberson 2018년 3월 3일
You cannot create a numeric matrix with that output. Your second row only has four numeric entries whereas the first and third have 5 entries.
You can create a text output that would display to look like what you want, but you cannot do it numerically.
You can create a cell array with one number per cell, and with empty cells, but you might not like the display.
  댓글 수: 1
Dhanushka Sandaruwan
Dhanushka Sandaruwan 2018년 3월 4일
Thanks Roberson. According to your comment I changed the code as follows.
Routes=[0 2 5 6 8 9 0 11 13 14 18 0 3 7 15 19 20 0];
Display={};
Current_Cluster=[];
for i=2:length(Routes)
if Routes(i) ~=0
Current_Cluster=[Current_Cluster Routes(i)];
else
Display=[Display , num2str(Current_Cluster)];
Current_Cluster=[];
end
end
disp('Culsters are');
Display'
Now it is working as I want but can't understand how it works. Anyway thanks a lot.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by