フィルターのクリア

How can I concatenate arrays in loop ?

2 ビュー (過去 30 日間)
Ender Rencuzogullari
Ender Rencuzogullari 2015 年 12 月 22 日
コメント済み: Ender Rencuzogullari 2015 年 12 月 22 日
Dear Contributers,
There is a loop;
for i = 1:n;
X_rotate = X.*cos(i*increment) - Y.*sin(i*increment);
Y_rotate = X.*sin(i*increment) + Y.*cos(i*increment);
Helix = [X_rotate(1:K1) ; Y_rotate(1:K1)];
fileID = fopen('helix_values.txt', 'w');
fprintf(fileID,'%f %f\n ', Helix);
fclose(fileID);
end
When open the text file, there just exists the last values of iteration X_rotate and Y_rotate. I need to collect the values for every iteration. I have tried to use cat command but I probably made mistake. How may I do that?
Thanks in advance.

採用された回答

Guillaume
Guillaume 2015 年 12 月 22 日
It looks like your X and Y are row vectors of size [1, K1] (inferred from the line Helix = [X_rotate(1:K1) ; Y_rotate(1:K1)];). In that case you don't need to use a loop to calculate your X_rotate and Y_rotate:
steps = 1:n;
X_rotate = bsxfun(@times, X, cos(steps*increment)') - bsxfun(@times, Y, sin(steps*increments)');
Y_rotate = bsxfun(@times, X, sin(steps*increment)') + bsxfun(@times, Y, cos(steps*increments)');
%rows of X_rotate and Y_rotate correspond to steps
%columns correspond to the original X and Y values
I'm unclear how you want to save all the iterations onto the same text file though, columns of X and Y multiplied by the number of iterations?
  8 件のコメント
Ender Rencuzogullari
Ender Rencuzogullari 2015 年 12 月 22 日
編集済み: Ender Rencuzogullari 2015 年 12 月 22 日
My bad. I was trying to say that I want to collect the values of X_rotate and Y_rotate from their first value to K1 th value. not all of them
Ender Rencuzogullari
Ender Rencuzogullari 2015 年 12 月 22 日
Alright, I appreciate for your help :)

サインインしてコメントする。

その他の回答 (1 件)

Star Strider
Star Strider 2015 年 12 月 22 日
I’m not certain what you’re doing, but to get the ‘X_rotate’ and ‘Y_rotate’ as vectors, you have to save them as such:
for i = 1:n;
X_rotate(i) = X.*cos(i*increment) - Y.*sin(i*increment);
Y_rotate(i) = X.*sin(i*increment) + Y.*cos(i*increment);
end
I don’t know what their actual dimensions are, so you might have to add a second dimension to each:
for i = 1:n;
X_rotate(i,:) = X.*cos(i*increment) - Y.*sin(i*increment);
Y_rotate(i,:) = X.*sin(i*increment) + Y.*cos(i*increment);
end
Do the file write after the loop, not in it.
  5 件のコメント
Star Strider
Star Strider 2015 年 12 月 22 日
I am not certain how your program is organised. However a while loop with a counter, or some other way to determine the size of the user’s inputs, for example asking the user, would be useful to define ‘n’.
Ender Rencuzogullari
Ender Rencuzogullari 2015 年 12 月 22 日
Thanks a lot Sir. I learnt that I can do it with cell arrays.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by