Writing calculation results into matrix

20 views (last 30 days)
Hi,
I am trying to write results into each matrix row c = zeros(4, length(a)). I have set of steps x = [1,2,3,4]. After the 1st five values for each step are calculated I want to write them into 4 rows of a single matrix. So, eventually it should be something like
c = ( 2 6 9 4 8 9 4 9 5 8
9 4 6 9 5 9 0 0 0 0
4 9 7 3 5 0 0 0 0 0
7 8 5 7 0 0 0 0 0 0)
I want then slice this matrix and make a single line plot which is also a question because I didn't try to do it so far. Therefore, any advises are very welcome. Thanks!
x = [1,2,3,4];
flag = 0;
for s = 1:length(x)
a = (1:x(s):50);
p = zeros(1,length(a));
c = zeros(4,length(a));
for i = 1:length(a)
p(i) = 3.*a(i) + 2;
v(i) = 4.*a(i) + 2;
if i == 5
c(1,:)=p
flag = 1;
break
end
end
if flag == 1
continue
end
end
  2 Comments
Stephen23
Stephen23 on 1 Mar 2015
Edited: Stephen23 on 1 Mar 2015
When I run your code the variable c has the following values
c =
5 17 29 41 53 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
which clearly does not match your desired output. So either the code or the explanation is wrong. If the code is wrong, then can you please provide a detailed explanation of what you want to calculate (i.e. the algorithm). But don't worry about the code itself: actually we know MATLAB reasonably well, but it does not help us if you provide us with broken code and don't tell us actually what you want it to do.
How are these numbers supposed to be calculated? In what order? Is there a general rule?
You desired output matrix is too complicated to know what the algorithm is just by looking at it, and broken code does not help us know the algorithm, so we need you to help us by explaining a bit more.
Oleg Stryzhyboroda
Oleg Stryzhyboroda on 1 Mar 2015
Edited: Oleg Stryzhyboroda on 1 Mar 2015
c = ( 2 6 9 4 8 9 4 9 5 8
9 4 6 9 5 9 0 0 0 0
4 9 7 3 5 0 0 0 0 0
7 8 5 7 0 0 0 0 0 0)
the numbers in the matrix above is not what it has to be as final result. It is only an example with random numbers. There can be any numbers
c = ( # # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #)
Sorry, it was really confusing.
My problem is that results for each new step are stored in different matrices. Like you said , for 1st step value from x-vector the result is
c =
5 8 11 14 17 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
for the 2nd , it is
c =
5 11 17 23 29 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
for the 3rd,
c =
5 14 23 32 41 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
and for 4th step
c =
5 17 29 41 53 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
What I want to achieve is that results are stored NOT in a separate matrices, as my case, but in one matrix. So eventually it should be like
c = ( 5 8 11 14 17
5 11 17 23 29
5 14 23 32 41
5 17 29 41 53)

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 1 Mar 2015
Edited: Stephen23 on 1 Mar 2015
When I ran your code, this is the output that it gives:
res =
5 8 11 14 17
5 11 17 23 29
5 14 23 32 41
5 17 29 41 53
With some minor changes to your code I also managed to get c to be the same matrix too. But rather than fixing that buggy code, here is a much neater and faster way to do it:
>> s = 4;
>> n = 5;
>> x = 1:s;
>> Y = cumsum([ones(s,1),repmat(x(:),1,n-1)],2);
>> Z = 3*Y + 2
Z =
5 8 11 14 17
5 11 17 23 29
5 14 23 32 41
5 17 29 41 53
The documentation will tell you how cumsum and repmat work. If you plan on using MATLAB you should learn about vectorization, lets you write faster, neater and easier code in MATLAB, without using loops.

More Answers (1)

Oleg Stryzhyboroda
Oleg Stryzhyboroda on 1 Mar 2015
Edited: Oleg Stryzhyboroda on 1 Mar 2015
So, I managed to end up with this solution. Are there any other ways to do it ?
x = [1,2,3,4];
flag = 0;
for s = 1:length(x)
a = (1:x(s):50);
p = zeros(1,length(a));
c = zeros(4,length(a));
for i = 1:length(a)
p(i) = 3.*a(i) + 2;
if i == 5
if s == 1
c(1,:) = p;
n1 = nonzeros(c);
end
if s == 2
c(2,:) = p;
n2 = nonzeros(c);
end
if s == 3
c(3,:) = p;
n3 = nonzeros(c);
end
if s == 4
c(4,:) = p;
n4 = nonzeros(c);
end
flag = 1;
break
end
end
if flag == 1
continue
end
end
res = [n1,n2,n3,n4]'
  1 Comment
Stephen23
Stephen23 on 1 Mar 2015
Edited: Stephen23 on 2 Mar 2015
Are there any other ways to do it ? Yes there are other ways to do this, and some of them are going to be much neater than this "solution". In MATLAB doing this using loops is a slow and buggy way to achieve this result. Look at my answer for a faster, neater and less buggy way to achieve this.
To place values into a matrix you should read about matrix indexing, which is covered in the documentation quite well. Here is an example of how this works:
N = 4;
X = nan(N,3);
for k = 1:N
X(k,:) = k + [0,0.1,100];
end

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!