Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

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 ??

44 Ansichten (letzte 30 Tage)
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 Kommentare
Stephen23
Stephen23 am 22 Nov. 2019
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.

Antworten (1)

Walter Roberson
Walter Roberson am 22 Nov. 2019
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 Kommentare
Walter Roberson
Walter Roberson am 22 Nov. 2019
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