Info

此问题已关闭。 请重新打开它进行编辑或回答。

Unable to perform assignment because the size of the left side is 1-by-10000 and the size of the right side is 1-by-20000. how to solve ??

1 次查看(过去 30 天)
clearvars;close all;clc;
count=0;
k=10e3:10e3:100e3;
for i=1:1:10
p(count+1,:)=rand(1,k(i));
count=count+1;
end
  2 个评论
Stephen23
Stephen23 2019-11-22
MATLAB arrays must be rectangular (i.e. cannot be "ragged"), so this allocation will not work because the number of elements changes on each iteration:
p(count+1,:)=rand(1,k(i));
You might be able to use a cell array.

回答(1 个)

Walter Roberson
Walter Roberson 2019-11-22
You are increasing the number of values generated for each element of the loop. How do you expect to store that continually increasing number of values as rows of a matrix? A matrix cannot have a different number of columns for each row.
  2 个评论
Walter Roberson
Walter Roberson 2019-11-22
k = 10e3:10e3:100e3;
p = [];
for i = 1:1:10
if size(p,1) < k(i)
p(end:k(i),1:i) = nan; %expands rows and columns
end
p(1:k(i), i) = rand(k(i), 1);
p(k(i):end, i) = nan;
end
This is not as efficient as it could be. More efficient would be to figure out the largest size ahead of time:
k = 10e3:10e3:100e3;
maxk = max(k);
N = 10;
p = nan(maxk, N);
for i = 1:1:N
p(1:k(i), i) = rand(k(i), 1);
end
Both of these pad the short columns with NaN to signal missing data.
Both versions are coded to account for the possibility that you might have shorter columns after longer columns but must still pad with nan properly.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by