필터 지우기
필터 지우기

matrix indexing and creating a new matrix with new size

조회 수: 2 (최근 30일)
Sobhan
Sobhan 2012년 6월 6일
Dear experts, I hope somebody can help me. I have a n*4 matrix. In a loop I want to index some rows (the range is not fixed) from column 1 and put them in a new matrix with separate columns. I guess since the size of rows are not the same, Matlab gives me the mismatch dimension error for new matrix! Any solution? Thanks in advance Sobhan

채택된 답변

Wayne King
Wayne King 2012년 6월 6일
How about
clear output
For f = 1:nfiles
IndexStart= RHs(RHs<peak);
IndexEnd= RHs(RHs>peak);
output{f} = Mydata (Index1:Index2,1);
end
If you know how big output is going to be in advance you can preallocate the cell array.
  댓글 수: 1
Sobhan
Sobhan 2012년 6월 6일
thanks Wayne,
your solution only gives me a cell array with the size of output and not a matrix.
Cheers

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

추가 답변 (3개)

Wayne King
Wayne King 2012년 6월 6일
How about using a cell array? Beyond that I think you will need to give us a little MATLAB example to illustrate your issue.
  댓글 수: 1
Sobhan
Sobhan 2012년 6월 6일
Hi Wayne,
This is a simplified version of my loop.
For f = 1:nfiles
IndexStart= RHs(RHs<peak);
IndexEnd= RHs(RHs>peak);
output (:, f) = Mydata (Index1:Index2,1);
End
only the first file can be saved in output but it gives error for the second file because it has different rows. Is it clear now? Sorry I am really new to matlab.
Cheers
Sobahn

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


Christoph
Christoph 2012년 6월 6일
Hi Sobhan,
as Wayne said, an example might be helpful. Nevertheless you might use:
X_new(:,end+1) = [X_old(:,i); zeros(size(X_new, 1) - size(X_old, 1),1)];
in your loop as long as the length of the old matrix is shorter or equal as the length of the new one. But waynes answer is the "politcally correct" one.
  댓글 수: 2
Sobhan
Sobhan 2012년 6월 6일
Hi,
Thanks for the answer. I gave an example above.
The length of the new matrix is sometimes bigger sometimes smaller. I think I need something to add a new column with the size of new file to previously created matrix. I am trying to figure it out.
Cheers
Christoph
Christoph 2012년 6월 6일
ok, try to implement the next few nasty codelines:
if length(X_new) < length(X_old)
X_new(end+1,:) = zeros(length(X_old)-length(X_new), size(X_new, 2))
end

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


Sobhan
Sobhan 2012년 6월 8일
Cell array was the solution: output{f} = Mydata (Index1:Index2,1)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by