필터 지우기
필터 지우기

Can I assign vectors with different number of rows per-column to a matrix?

조회 수: 6 (최근 30일)
I have an optimization function that outputs a vector. I included this optimization function in a for loop so it runs for length(T) times resulting in length(T) number of vectors. The problem is that the vectors have different number of rows and I want to store them in sometihng like a matrix or table for ex so I can use the values later for other operations later. However this is not possible since for a matrix the number of rows in all columns need to be the same. I added comments next to the lines for clarification
Example
1- N=[8 8 8 7 5 6 9 8 6 4 7 6 8 6 4 7 9] %%%Length(T)=length(N) This is okay
2- P=zeros(2*max(N),length(T))?????? How can I prelocate P from line 8.
3- for iii=1:length(T) %THis line is okay
4- E1=transpose(cost1ext( :,iii )); %%THis line is okay
5- E2=transpose(cost2ext(:,iii )); %THis line is okay
6- tand_measuree=transpose(cost3ext(:,iii)); %This line is okay
7- save('there',"tand_measuree","E2","E1") %THis line is okay
8- P(:,iii)=CURVE_FITopti(N(iii),Startg,Starttau,ubgi,ubtaui,Dec_E)
9- end
every run the vector P resulting from the execution of line 8 will be a vector with 2*N(iii) rows. I was trying to prelocated P as a matrix P=zeros(2*max(N),length(T)) so that after the loop is finished I will have a matrix with some zeros left for the shorter vectors, but it did not work.
<stopping criteria details>
Unable to perform assignment because the size of the left side is 18-by-1 and the size of the right side is 16-by-1.
Error in untitled6 (line 90)
P(:,iii)=CURVE_FITopti(N(iii),Startg,Starttau,ubgi,ubtaui,Dec_E)

채택된 답변

Star Strider
Star Strider 2024년 1월 13일
I would use a cell array. It is not as convenient to use as a numeric matrix, however it tolerates different length vectors.
  댓글 수: 4
Abdallah Magour
Abdallah Magour 2024년 1월 14일
편집: Abdallah Magour 2024년 1월 14일
Hi thanks Star, yes it works well.

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

추가 답변 (1개)

David Goodmanson
David Goodmanson 2024년 1월 14일
Hi Abdallah,
Cell arrays are very versatile but they are also on the slow side. Depending on what you are doing, that may not matter at all (in which case it's a good opportunity to gain some familiarity with cells), or it might. Another way to do this is, if you know the length of the longest vector or you have a reasonable estimate of the maximum possible length, you can pad the end of each vector with a value that you know is not any element of the vectors themselves, such as NaN for example. Here is an example.
m = nan(6,10); % 10 is a known bound for the max length
% create and store vectors
m(1,1:5)= randi(9,1,5);
m(2,1:8)= randi(9,1,8)
m(3,1:4)= randi(9,1,4)
m(4,1:6)= randi(9,1,6)
m(5,1:7)= randi(9,1,7)
m(6,1)= 17;
m
m =
4 5 4 1 3 NaN NaN NaN NaN NaN
2 2 3 4 1 9 9 5 NaN NaN
5 4 9 4 NaN NaN NaN NaN NaN NaN
2 8 4 3 4 1 NaN NaN NaN NaN
2 9 9 6 1 3 4 NaN NaN NaN
17 NaN NaN NaN NaN NaN NaN NaN NaN NaN
It's useful to know the length of each vector without explictily having to look at m, so
veclength = sum(~isnan(m),2)
veclength =
5
8
4
6
7
1
  댓글 수: 1
Abdallah Magour
Abdallah Magour 2024년 1월 14일
Hi David, thanks for your reply. I treid to use the nan matrix but it unfortunatley did not not work. However, it is still usefull info for other applications.

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

카테고리

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

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by