I have a matrix like this
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
5 6 7 8
5 6 7 8
5 6 7 8
5 6 7 8
9 10 11 12
9 10 11 12
9 10 11 12
9 10 11 12
like that so many rows.I want to extract every 4 row and 4 column continuously for whole row and save it in excel file.For a single row it works b=a(1:1:4),:);but for all row how can it possible.

 채택된 답변

Guillaume
Guillaume 2016년 5월 3일

0 개 추천

Do you mean?
b = a(1:4:end, :)

댓글 수: 5

It will take every 4 th row.I want 1 to 4 row each time for 365 rows as I have 365 rows.
I really have no idea what you mean. Can you provide an example of the output you want?
b = a(1:4, :)
will give you row 1 to 4. I don't understand what "each time for 365 rows" mean.
if true
% code
endSir one file will contain
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
another file will contain
5 6 7 8
5 6 7 8
5 6 7 8
5 6 7 8
third file will contain
9 10 11 12
9 10 11 12
9 10 11 12
like that 365 files will be generated with 4 row and column. For every 4 th row one file will be created.
if true
% code
end
Guillaume
Guillaume 2016년 5월 3일
편집: Guillaume 2016년 5월 3일
I believe this is what you want:
assert(mod(size(a, 1), 4) == 0, 'number of rows is not a multiple of 4');
numcells = size(a, 1) / 4; %number of matrices generated
b = mat2cell(a, ones(1, numcells)*4, size(a, 2)); %split a into matrices
%save as excel file however you want, e.g.:
savepath = 'C:\wherever\you want\to save your files\');
for bidx = 1:numcells
xlswrite(fullfile(savepath, sprintf('%4d.xlsx', bidx)), b{bidx});
end
Sir it is really fantastic.It works.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2016년 5월 3일

0 개 추천

Guillaume's first answer gave you every 4th row extracted from the original, and all columns will be included. Perhaps when you say you "want to extract every 4 row and 4 column" you want this:
m4 = m(1:4:end, 1:4:end);
or
m4 = imresize(m, 0.25, 'nearest');
This will subsample the matrix by 4 in both the row and column direction.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by