Using horzcat in a loop for combining large number of files

조회 수: 1 (최근 30일)
Sagar
Sagar 2017년 7월 27일
편집: Constance Woodman 2017년 7월 27일
hi, I need to combine 100 matrices horizontally using horzcat. I have a matrix named 'data' of size 20*30*100 in which 100 represents number of data granules for a day. I want to do the following: data_all = horzcat(data(:, :, 1), data(:, :, 2), data(:, :, 3) ................... upto 100). I want to do this in a loop since it is tedious to write all matrices from 1 to 100. Could you please suggest a quick way?
  댓글 수: 1
Constance Woodman
Constance Woodman 2017년 7월 27일
편집: Constance Woodman 2017년 7월 27일
I'm not super elegant at this but would think you would want to do it in one command rather than adding a column, then adding a column, etc., as a loop. I have been working with huge datasets and it can crash matlab on my older work machine when I make Matlab do extra work.
Maybe rather than horizontally concatenating in a loop, you could create a loop that writes the list for concatenation, then just insert the list?
I'm just sort of pseudo coding this out...
% initilize
x = total number columns
counter= 1
hugeFlippingString = empty string
% run loop
Loop until counter = total number of columns
append text to hugeFlippingString: "columnOrFileName", counter, ","
counter increment by 1
End loop
%Concatenate
giantMatrix = horzcat(hugeFlippingString)
% which actually does this: giantMatrix = horzcat(col1,col2, ... col100)

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

채택된 답변

James Tursa
James Tursa 2017년 7월 27일
편집: James Tursa 2017년 7월 27일
data_all= reshape(data,size(data,1),[]);
Or, if you needed to vertcat them instead, then you could use
result = reshape(permute(data,[2 1 3]),size(data,2),[])';

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by