How do i make this code as simple as possible? :(

final_0 = 0;
final_1 = final(numel(final_0)+1:(floor(numel(final)*1/10))*1);
final_2 = final(floor(numel(final)*1/10)*1+1:floor(numel(final)*1/10)*2);
final_3 = final(floor(numel(final)*1/10)*2+1:(floor(numel(final)*1/10))*3);
final_4 = final(floor(numel(final)*1/10)*3+1:(floor(numel(final)*1/10))*4);
final_5 = final(floor(numel(final)*1/10)*4+1:(floor(numel(final)*1/10))*5);
final_6 = final(floor(numel(final)*1/10)*5+1:(floor(numel(final)*1/10))*6);
final_7 = final(floor(numel(final)*1/10)*6+1:(floor(numel(final)*1/10))*7);
final_8 = final(floor(numel(final)*1/10)*7+1:(floor(numel(final)*1/10))*8);
final_9 = final(floor(numel(final)*1/10)*8+1:(floor(numel(final)*1/10))*9);
final_10 = final(floor(numel(final)*1/10)*9+1:floor(numel(final)));
I think anyone who has studied a little bit can make it simple, but I'm not
For your information, the class of those codes is char.
The size of one data is so large that you want to split it into 10 pieces.
Please let me know, and if you have time, I would like to know if this code can be expressed in loop. :((((((((

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 12월 30일

0 개 추천

First, naming variables like final_1, final_2, ... is a bad coding practise: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. Always use arrays. In your case, cell arrays are most appropriate.
You can write the code in a single line like this
final = .. % your arrat
final_split = mat2cell(final, 1, numel(final)/10*ones(1,10))

댓글 수: 5

Name Size Bytes Class Attributes
final 1x1102272 2204544 char
I'm sorry. I forgot this.
This is information about the array I'm trying to split.
I used the floor function because this array is not divided by 10.
Also, mat2cell is not available due to errors in the overall size of the matrix.
Yes, In that case what do you want to do to the last 2 elements? Following code show how to ignore them
final = rand(1, 1102272); % example array
final_ = final(1:floor(end/10)*10);
final_split = mat2cell(final_, 1, numel(final_)/10*ones(1,10))
Jungwu Kim
Jungwu Kim 2020년 12월 31일
thank you so much!! It was very helpful. but I want to designate each matrix so that it can be called...
Ameer Hamza
Ameer Hamza 2020년 12월 31일
편집: Ameer Hamza 2020년 12월 31일
Just use indexing to access each element of the cell array. There is no need to create seperate variables. For example
final_split{1} % access first element
final_split{2} % access second element
..
..
final_split{end} % access last element
Jungwu Kim
Jungwu Kim 2020년 12월 31일
thank you!! god bless you and happy new year~

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 12월 30일

1 개 추천

buffer(final, floor(numel(final)/10)).'
Needs the signal processing toolbox.
The final (11th) row will contain 110227 entries only the first two of which will be nonblank. You might want to consider using ceil instead of floor which will get you 10 rows the last of which has 8 padding positions.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2020년 12월 30일

댓글:

2020년 12월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by