Matrix with different order

I want to use 10*1 matrix for N=10, now as I keep changing N =20,30,40,50, matrix order must also change like, 20*1,30*1,40*1,50*1... using rand(N,1) I can produce required array. My problem is I am using a for loop, N=10:10:50 And I want to use matrix of order N*1 defined Outside of this loop. Kindly help how to write the code.

6 Comments

DGM
DGM on 14 Jul 2021
Edited: DGM on 14 Jul 2021
All we know is that you want a vector of a specified length. What exactly is this vector? Is it ones? zeros? random numbers? Is it some sort of data from another source?
I have to ask, since ones(), zeros(), and all the random number generator functions clearly describe how to specify the output size. If you're asking the question, one is inclined to assume you must want something other than the obvious.
It is array of complex random numbers.
Yes, using rand(N,1) I can produce required array. My problem is I am using a for loop, N=10:10:50 And I want to use matrix of order N*1 defined Outside of this loop.
If you want a fixed N as the size of the random array, then don't use that variable as the loop index -- define a different variable for each needed purpose; don't try to make one variable do the job of two.
nR=10; nC=1; % define the matrix size row by column
for N=10:10:50 % now create the loop over N
M=rand(nR,nC); % create the random matrix of fixed size independent of loop
... % do whatever with M here
end
DGM
DGM on 14 Jul 2021
Edited: DGM on 14 Jul 2021
If your vector needs to be of variable length, based on the state of the loop, why can't you do it in the loop? You'd need to precalculate all the vectors otherwise. You could store them in a cell array, but why set up one loop to build an array of variable length vectors just to avoid generating them at the time of use in another loop?
Depending on your needs, you may be able to calculate one oversize vector and then generate the shorter vectors by sampling from it, but I don't see how that's going to be easier than just doing it in the loop.
Thank you Sir, I will be trying it.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 14 Jul 2021

Commented:

on 14 Jul 2021

Community Treasure Hunt

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

Start Hunting!