Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

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 vue (au cours des 30 derniers jours)
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 commentaires
Stephen23
Stephen23 le 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.
pavan kumar
pavan kumar le 22 Nov 2019
thank u sir..
please suggest me how to use a cell array ??

Réponses (1)

Walter Roberson
Walter Roberson le 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 commentaires
pavan kumar
pavan kumar le 22 Nov 2019
thank u sir.
sir, how can i store differernt coloumns in a same matrix ??
Walter Roberson
Walter Roberson le 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.

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by