taking samples from a vector

I have the vector psi. It has too many items and I cannot copy and paste the values in a txt file to plot a graph in Latex.
Would be great to have a small code to take a sample like every 5 or 10 items to reduce the size and create a new file psi_short.
It won't lose the shape since its dicretization is very high.
I wrote this but it doesn change anithyng:
for i=1:length(psi)
psi_short(i) =psi(i);
i=i+5;
end

1 Comment

Use my code.
It is not possible to change the index in a for loop. The for function simply ignores the changes (although you can use changed values for it in calculations).
Even if it worked, the explicit loop is much less efficient than the indexing approach my code uses.

Sign in to comment.

 Accepted Answer

Try these:
D = load('psi.mat');
psi = D.psi; % (10962 x 1)
Every5 = psi(1:5:end); % (2193 x 1)
Every10 = psi(1:10:end); % (1097 x 1)
.

4 Comments

thanks a lot, but it's not working.
It doesn't load the file:
Error using load
Unable to read file 'psi.mat'. No such file or directory.
I don't know if it matters but I have Matlab 2014b
If you already have ‘psi’ in your workspace, you likely do not need to load the file. (I needed to import it to work with it.)
Otherwise, you simply need to find the file (this is operating-system dependent, in Windows 10 you would use ‘File Explorer’) and then move or copy it to a directrory somewhere in your MATLAB user path. See: What is the MATLAB Search Path? for details. I do not have access to your computer, so I cannot help you further with the file location problem. With respect to the file location problem, I doubt the version is significant.
Thanks again a lot. I was making such a stupid mistake after a long day in front of the laptop.
Have a great day and thank yoou again.
Thank you!
As always, my pleasure!
I don’t consider mistakes stupid if their solution is instructive!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2014b

Community Treasure Hunt

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

Start Hunting!