How to extract values from the loop and plot them in matlab? - loops

This is my simple code:
for i=-20:20;
s=[-1 0 0];
e=[1 0 0];
r=[i 5 0];
b=e-r;
a=s-r;
w=cross(a,b);
y=dot(w,w);
z=dot(a,b);
u=norm(a);
v=norm(b);
k=dot(u,v);
g=1;
q=(w/y)*(u+v)*(1-z/k);
V=g/4*pi*q
end
But even such simple i can not figure out how to plot the results (only Z components of the vector V). Please help?

1) Create a figure f=figure();
2) Put hold on; command such that you draw the points in the same figure without overwriting the previous plot commands;
3) run the loop and use a plot of your choice. such as plot(i,z,'o'); here you draw a circle
Cheers
TL

Related

Create array of scatter plots with matplotlib [duplicate]

This question already has answers here:
How to assign a plot to a variable and use the variable as the return value in a Python function
(3 answers)
Closed 5 months ago.
I'm currently creating scatters plot to represent the spin orientation for a ferromagnetic material, but this changes with time. What I'm trying to do is create the plots in a for loop. I can currently create each of the plots, but I don't know how to store and recall the plots I'm producing.
Ideally, I would like to recall a specific plot later in the document without having to save the plot as an image.
Is this at all possible?
I am currently using SageMath and Jupyter Notebooks.
This is currently my code to produce the scatter plots:
Beta = ellipsis_range(0.05,Ellipsis,0.8,step=0.05)
for i in range(len(Beta)):
B = Beta[i]
j = 0
if B<0.85:
plt.figure(figsize=((9,6)))
for y in range(1,N+1):
for x in range(1,N+1):
if state[j]==1:
plt.scatter(x,y,color="red",marker="^")
if state[j]==-1:
plt.scatter(x,y,color="blue",marker="v")
j = j+1
plt.title("Spin Distribution for Beta="+str(round(B,2)))
plt.show()
My eventual goal is to animate these plots.
Thank you in advance!
I have since managed to store my scatter plots as an array!
def spinplot(B,j,N):
plot = plt.figure(figsize=((9,6)))
for y in range(1,N+1):
for x in range(1,N+1):
if state[j]==1:
plt.scatter(x,y,color="red",marker="^")
if state[j]==-1:
plt.scatter(x,y,color="blue",marker="v")
j = j+1
plt.title("Spin Distribution for Beta="+str(round(B,2)))
return plot
In the original for-loop, I replaced the plotting code with:
j = 0
if B<0.85:
plots[k] = spinplot(B,j,N)
plt.close()
k = k+1
I am now trying to animate them. Which I will ideally save as a gif.

How to add element to array in MatLab?

I am trying to make a graph of the brightness of a pixel vs the distance from center of that pixel. To do so I used for loops to check each pixel for these values. But when adding them to my array I find that I can't. One of the issues is I have to define the array size first so no values get placed in the right spot. I believe everything else to be working except adding values to the arrays.
I've tried various methods of concatenation to add the values of each pixel to the array. I didn't have any more solutions to try.
folder3 = 'C:\Users\slenka\Desktop\Image_Analysis\Subtracted';
cd('C:\Users\slenka\Desktop\Image_Analysis\Subtracted');
subtractedFiles = [dir(fullfile(folder3,'*.TIF')); dir(fullfile(folder3,'*.PNG')); dir(fullfile(folder3,'*.BMP')); dir(fullfile(folder3,'*.jpg'))];
numberOfSubImages= length(subtractedFiles);
for b = 1 : numberOfSubImages
subFileName=fullfile(folder3, subtractedFiles(b).name);
chartImage=imread(subFileName);
[chartY, chartX, chartNumberOfColorChannels] = size(chartImage);
ccY= chartY/2;
ccX= chartX/2;
c=[ccX,ccY];
distanceArray=zeros(1,chartX);
intensityArray=zeros(1,chartY);
f=1;
g=1;
for y=1:chartY
for x=1:chartX
D = sqrt((y - c(1)) .^ 2 + (x - c(2)) .^ 2);
grayScale= impixel(chartImage, x, y);
distanceArray(f)=[D];
intensityArray(g)=[grayScale];
f=f+1;
g=g+1;
end
end
xAxis=distanceArray;
yAxis=intensityArray;
plot(xAxis,yAxis);
end
I'm expecting 2 arrays one full of the data values for the light intensity of each pixel in the image, and another for that pixels distance from the center of the image. I am wanting to plot these two arrays as the y and x axis respectively. At the moment the actual results is an entirely empty array full of zeros.

Matlab: Plot array such that each value has random shape and a color map

In Matlab:
How do I modify plot(x,y,'o'), where x=1:10 and y=ones(1,10), such that each point in the plot will have a random shape?
And how can I give it colors chosen from a scheme where the value at x=1 is the darkest blue, and x=10 is red (namely some sort of heat map)?
Can this be done without using loops? Perhaps I should replace "plot" with a different function for this purpose (like "scatter"? I don't know...)? The reason is that I am plotting this inside another loop, which is already very long, so I am interested in keeping the running-time short.
Thanks!
First, the plain code:
x = 1:20;
nx = numel(x);
y = ones(1, nx);
% Color map
cm = [linspace(0, 1, nx).' zeros(nx, 1) linspace(1, 0, nx).'];
% Possible markers
m = 'o+*.xsd^vph<>';
nm = numel(m);
figure(1);
hold on;
for k = 1:nx
plot(x(k), y(k), ...
'MarkerSize', 12, ...
'Marker', m(ceil(nm * (rand()))), ...
'MarkerFaceColor', cm(k, :), ...
'MarkerEdgeColor', cm(k, :) ...
);
end
hold off;
And, the output:
Most of this can be found in the MATLAB help for the plot command, at the Specify Line Width, Marker Size, and Marker Color section. Colormaps are simply n x 3 matrices with RGB values ranging from 0 to 1. So, I interpreted the darkest blue as [0 0 1], whereas plain red is [1 0 0]. Now, you just need a linear "interpolation" between those two for n values. Shuffling the marker type is done by simple rand. (One could generate some rand vector with size n beforehand, of course.) I'm not totally sure, if one can put all of these in one single plot command, but I'm highly sceptical. Thus, using a loop was the easiest way right now.

Create an array 1*3 containing only one 1 and rest 0

I am just learning matlab now. I faced a difficulty in creating an array of 3 elements in a row.
I wrote a code
Source = randi ([0,1],1,3);
which gave me output
[1,1,0].....
[0,1,1]....
but I was willing to get only one 1 and two zeros in the output instead of getting two 1 and one zero.
I know I am wrong because I am using randi function and gives random value of 0 & 1 and output I get can be [0,0,1] ... [1,0,0]... too.
My clear problem is to only get only one 1 if I repeat as many times. e.g. I should get only [0,0,1] or [0,1,0] or [1,0,0].
Hope I can get solution.
Thank you.
Ujwal
Here's a way using randperm:
n = 3; %// total number of elements
m = 1; %// number of ones
x = [ones(1,m) zeros(1,n-m)];
x = x(randperm(numel(x)));
Here is a couple of alternative solutions for your problem.
Create zero-filled matrix and set random element to one:
x = zeros(1, 3);
x(randi(3)) = 1;
Create 1x3 eye matrix and randomly circshift it:
x = circshift(eye(1,3), [0, randi(3)]);

gnuplot for loop with interval

I have the following gnuplot script that uses a for loop to plot 100 data sets of (x,y) format to one plot. However, the script only plots 2 data sets. Can anybody help? Thank you.
plotfile = "graph.eps"
set output plotfile
filename(n) = sprintf("%d_mod.int", n)
plot for [i = 400000:4000000:400000] filename(i) u 1:2 title sprintf("%d", i) w lp
That is a bug, which will be fixed in 4.6.6 and 5.0, see #1429 Erratic behaviour of do for loops .
As a workaround you must iterate over smaller numbers:
plot for [i = 4:40:4] filename(i*100000) u 1:2 title sprintf("%d", i*100000) w lp

Resources