how to write this for cycle

조회 수: 3 (최근 30일)
salvatore liberto
salvatore liberto 2016년 11월 10일
댓글: Thorsten 2016년 11월 10일
Hi everyone, i need to create this for cycle:
x(64) = 0:1/64:1;
x(128) = 0:1/128:1;
.
.
.
x(4096) = 0:1/4096:1;

답변 (6개)

Star Strider
Star Strider 2016년 11월 10일
I’m not certain what you want. It is straightforward to define an anonymous function to do that:
x = @(n) 0 : 1/n : 1;
and to call it, for example:
xv64 = x(64);
and so for the others.
  댓글 수: 4
Star Strider
Star Strider 2016년 11월 10일
You can have a constant step, or a constant length, but not both.
The best you could do is to store them in a cell array, or create a matrix of NaN values equal to the length of the longest vector, and then fill the matrix rows with shorter vectors.
For example:
x = @(n) 0 : 1/n : 1;
M = nan(7, length(x(2^12)));
for k1 = 1:size(M,1)
v = x(2^(k1+5));
lv = length(v);
M(k1,1:lv) = v;
end
This is likely the most efficient way to create a matrix.
What do you want to do?
Guillaume
Guillaume 2016년 11월 10일
As I pointed out
"every matrix contains n (=2^i) values which vary from 0 to 1, with a step of 1/(2^i)"
is not possible. You either have n+1 values with a step of 1/n, or you have n values with a step of 1/(n-1), or the values go from 0 to 1-1/n to get n values with 1/n step.

댓글을 달려면 로그인하십시오.


Guillaume
Guillaume 2016년 11월 10일
arrayfun(@(n) linspace(0, 1, n), 2.^(6:12), 'UniformOutput', false)
will create 7 (not 6!) matrices with the exponent varying from 6 to 12, stored in a cell array
Note that each matrix will have n elements and therefore the step will be 1/(n-1) not 1/n (as it's not possible to generate n elements from 0 to 1 included with a step of 1/n)
  댓글 수: 1
Guillaume
Guillaume 2016년 11월 10일
If you're really insistent you want a step of 1/n, then:
x = arrayfun(@(n) linspace(0, 1, n+1), 2.^(6:12), 'UniformOutput', false); %n+1 elements will create a step of 1/n
If you're really insistent that you want the 7 matrices at indices 64,128, 256, ..., therefore wasting plenty of memory:
x(2.^(6:12)) = arrayfun(@(n) linspace(0, 1, n), 2.^(6:12), 'UniformOutput', false); %or n+1 in linspace

댓글을 달려면 로그인하십시오.


salvatore liberto
salvatore liberto 2016년 11월 10일
In this way it is certainly more clear.
I have a n = 64.
and
x=0:h:1; {h = 1/n)
i want to create a for loop that, instead of writing the same for loop (changing the number of n, and the name of the variable), generates 6 matrices [n*1]; more specifically, this six:
x(64) = 0 : h : 1; {h = 1/64}
x(128) = 0 : h : 1; {h = 1/128}
x(256) = 0 : h : 1; {h = 1/256}
x(512) = 0 : h : 1; {h = 1/512}
x(1024) = 0 : h : 1; {h = 1/1024}
x(2048) = 0 : h : 1; {h = 1/2048}
x(4096) = 0 : h : 1; {h = 1/4096}
  댓글 수: 1
Guillaume
Guillaume 2016년 11월 10일
편집: Guillaume 2016년 11월 10일
The arrayfun function I used in my answer is exactly equivalent to a for loop, it just requires less typing and is in my opinion easier to read.
If you're really insistent you want a step of 1/n per matrix (and therefore n+1 numbers), then replace the n in the linspace call by n+1.
As also pointed out your 6 matrices are actually seven matrices.

댓글을 달려면 로그인하십시오.


Thorsten
Thorsten 2016년 11월 10일
편집: Thorsten 2016년 11월 10일
You have to use a cell array because your indices have different sizes
e = 6:12;
for i = 1:numel(e), x{2^e(i)} = 0:1/2^e(i):1; end
if you do not need to address x{64}, x{128}, ... but if you can also use x{1}, x{2}, ... you can save some memory by using
for i = 1:numel(e), x{i} = 0:1/2^e(i):1; end
  댓글 수: 1
salvatore liberto
salvatore liberto 2016년 11월 10일
the address is for ease of reading.

댓글을 달려면 로그인하십시오.


salvatore liberto
salvatore liberto 2016년 11월 10일
Cell contents assignment to a non-cell array object. This is that appears with the code of thorsten
  댓글 수: 1
Thorsten
Thorsten 2016년 11월 10일
Use
clear x
before you run the code.

댓글을 달려면 로그인하십시오.


salvatore liberto
salvatore liberto 2016년 11월 10일
it's not necessary to write x(64) but also x64
  댓글 수: 1
Guillaume
Guillaume 2016년 11월 10일
편집: Guillaume 2016년 11월 10일
Please use Comment on this Answer to actually comment on an answer, not the Answer this question box. It's impossible to follow the thread of your comments

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by