How can I have the numbers in one vector after a while loop?

Let say that I have a matrix A=[1 0 5 4; 2 0 5 4; 3 4 6 2]; and I am using the below code ( see my previews question Plot the numbers of a matrix as points (dots) in a plot) while any(A(:) > 0) [r,c] = find(A>0); X= randn; Y= randn; a=c+X b=r+Y A = A-1; end
it returns for a and b a =
1.4471
1.4471
1.4471
2.4471
3.4471
3.4471
3.4471
4.4471
4.4471
4.4471
b =
1.5364
2.5364
3.5364
3.5364
1.5364
2.5364
3.5364
1.5364
2.5364
3.5364
a =
0.6514
0.6514
1.6514
2.6514
2.6514
2.6514
3.6514
3.6514
3.6514
b =
3.1841
4.1841
4.1841
2.1841
3.1841
4.1841
2.1841
3.1841
4.1841
a =
1.2058
2.2058
3.2058
3.2058
3.2058
4.2058
4.2058
b =
3.1324
3.1324
1.1324
2.1324
3.1324
1.1324
2.1324
a =
1.4771
2.4771
2.4771
2.4771
3.4771
3.4771
b =
3.9398
1.9398
2.9398
3.9398
1.9398
2.9398
a =
1.6379
1.6379
1.6379
b =
0.2081
1.2081
2.2081
a =
3.6630
b =
3.5855
how can I have all the numbers of a and b in one vector respectively ?

 Accepted Answer

Although it is slightly different in operation, you could try this:
A = [1 0 5 4; 2 0 5 4; 3 4 6 2];
N = sum(A(:));
X = randn(N,1)/12;
Y = randn(N,1)/12;
[R,C] = find(A>0);
B = A(A>0);
X = X + cell2mat(arrayfun(@(z,m)repmat(z,m,1),C,B,'UniformOutput',false));
Y = Y + cell2mat(arrayfun(@(z,m)repmat(z,m,1),R,B,'UniformOutput',false));
scatter(X,Y,[],randperm(N),'filled')
axis(gca,'ij')
colormap('winter')
which generates this figure:
The main differences from your code is that it generates all values at once (in the vectors X and Y) rather than within a loop, and it also uses a different random position for each point (rather than applying one random value to all points at each step of the while-loop). The code assumes that all values of A are positive integers or zero.

4 Comments

Thanks you so much for your help. What I want actually is to have the scatter according to A =
1 0 5 4
2 0 5 4
3 4 6 2 matrix and not from B therefore for instance in the position i,j= 3,3 in A matrix I want 6 dots in the scatter and the coordinates for each point and so on
@Ang Vas: I do not understand your comment, as the code I gave seems to plot exactly what you are describing: it plots the values of A as points, e.g. in position (3,3) it has six dots.
When you read the code you will find the line B = A(A>0), which is what is then plotted: values from A, first extracted into a temporary variable named B.
Note that you can choose another colormap if you want these dots to be more easily discernible. You can also adjust the division values 12, to change how spread out the dots are from each other.
EDIT: Another way to increase the dot-discernability is to replace randn with rand, like this:
X = (0.5-rand(N,1))/3;
Y = (0.5-rand(N,1))/3;
I am so sorry about this!!!. I am amateur in matlab and i am still haven't found my roots with it . Thanks a lot for you help
There is nothing wrong with being a beginner! Everyone here on this forum was once a beginner... it just takes time and lots of practice to learn a tool like MATLAB:
  • Read lots of examples of good MATLAB code
  • Learn to use the documentation.
  • Use this forum to learn some good techniques.
Good luck and have fun with MATLAB!

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Asked:

on 10 Jun 2015

Edited:

on 10 Jun 2015

Community Treasure Hunt

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

Start Hunting!