Copying from one array to another

조회 수: 12 (최근 30일)
R J
R J 2014년 9월 20일
댓글: Star Strider 2014년 9월 21일
Hi,
I have a data.txt file that I import using txt2mat. The relevant data are in chunks of length 1250 or 1500 points, but when I try to copy them to an array I get a "Subscripted assignment dimension mismatch" error.
For example: A = [1;2;3;4;5] and B = [1;2;3;4;5;6] If I want to copy the contents of A and B to temp(1,:) and temp(2,:), respectively, I get the error. Is it possible for me to store the contents of A and B, although they are different lengths, in the same temp array?
Any help in handling this would be appreciated.. Thanks!
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2014년 9월 20일
You know they are different sizes, then what do you want?
John
John 2014년 9월 20일
When you do an assignment like that, the rows have to be the same size. Consider using a cell. Or preallocating temp to be big enough to hold the biggest row and then storing the rows A and B after padding them with NaNs

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

채택된 답변

Star Strider
Star Strider 2014년 9월 20일
As a cell array: yes. As a numeric array: no, unless you preallocate an array with a row length the size of the longest row vector.
Example:
A = [1;2;3;4;5];
B = [1;2;3;4;5;6];
% Cell Array:
tempc = {A B};
% Numeric Array
tempn = nan(max(size(A,1),size(B,1)),2); % Preallocate NaN Matrix
tempn(1:size(A,1),1) = A;
tempn(:,2) = B;
  댓글 수: 2
R J
R J 2014년 9월 21일
Ahh, ok I see. Thanks for clarifying this Star!
Star Strider
Star Strider 2014년 9월 21일
My pleasure!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by