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

3 次查看(过去 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 个评论

请先登录,再进行评论。

更多回答(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 中查找有关 Logical 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by