How can I concatenate arrays in loop ?

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.

 Accepted Answer

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 Comments

Dear Guillaume,
X and Y have lots of values. let assume, both X and Y have fifty values at first. X_rotate and Y_rotate find another fifty values for every iteration until n. and let say n=10. Thus, the text file should has 500 values for X_rotate, 500 values for Y_rotate. I hope I could explan well.
Yes, but the question is in what order should these 500 values be written to the text file?
  • You could have 10 x 2 columns with 50 rows: X(n = 1), Y(n = 1), X(n=2), Y(n=2), ... Y(n = 10)
  • You could have 10 x 2 columns with 50 rows: X(n = 1), X(n = 2), ..., X(n = 10), Y(n = 1), ... Y(n = 10)
  • You could have 500 rows with two columns if you put the different steps below each others
  • something else?
the first one that you said.
%for first iteration;
X(1) Y(1)
X(2) Y(2)
.
.
X(K1) Y(K1)
%for second iteration;
X(1) Y(1)
X(2) Y(2)
.
.
X(K1) Y(K1)
so on.. those X(1:K1) and Y(1:K1) values should be in text n times. Briefly, 2 coloumns, n x K1 rows.
Ok, slightly modified bsxfun section from my answer to make it easier to write the data to file:
steps = 1:n;
cossteps = cos(steps * increment);
sinsteps = sin(steps * increment);
X_rotate = bsxfun(@times, X(:), cossteps) - bsxfun(@times, Y(:), sinsteps);
Y_rotate = bsxfun(@times, X(:), sinsteps) + bsxfun(@times, Y(:), cossteps);
%rows and columns are swapped compared to my original answer
%columns of X_rotate and Y_rotate now correspond to steps
%rows now correspond to the original X and Y values
Helix = [reshape(X_rotate, 1, []); reshape(Y_rotate, 1, [])];
%same code as you had to write the file:
fileID = fopen('helix_values.txt', 'w');
fprintf(fileID,'%f %f\n ', Helix);
fclose(fileID);
Sir, I realy appreciate it. It is almost fit. I will modify it. forgive me, I need to ask that I want to rows correspond to X_rotate(1:K1) and Y_rotate(1:K1). Is it possible ? by the way, lots of thanks !
"I want the rows correspond to X_rotate(1:K1) and Y_rotate(1:K1)"
Unless I'm not understanding you, this is the case. The file is made up of 500 rows with two columns.
edit: Maybe you're opening the file in lowly notepad that does not show you the rows because it's not recognising the simple line ending you're using. Opening the file in any other program (notepad is really dumb), including matlab would show you the rows.
You can modify your code to output the proper line ending simply by modifying this line:
fileID = fopen('helix_values.txt', 'wt'); %note the addition of 't' to specify text file
%this will automatically translate the '\n' in fprintf to '\r\n' on windows.
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
Alright, I appreciate for your help :)

Sign in to comment.

More Answers (1)

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 Comments

Sir, I have tried it before. However, There existed problem that the number of elements must be the same
Without knowing the sizes of the individual elements of ‘X_rotate’ and ‘Y_rotate’, I cannot write the appropriate loop code for it.
One option would be to save them as cell arrays:
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
Note the substitution of curly braces ‘{}’ for parentheses ‘()’.
You are right sir. However, the sizes of X_rotate and Y_rotate are changing when the program is runned according to user's inputs. I cannot define the size permanently. Should you recommend something for this situation?
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’.
Thanks a lot Sir. I learnt that I can do it with cell arrays.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!