recording matrix increasing in another matrix.

2 次查看(过去 30 天)
Hi friends,
I have a simple question,
A = [0.8888 0.5555 0.4444 0.7777 0.6666 0.2222 0.9999 0.1111];
B = 0.1;
While S=size(C,1) C=A+B; S=sum(C(:)>=1); B=B+0.1; end
in the above loop, the "C" will change to C=[0.9888 0.6555 0.5444 0.8777 0.7666 0.3222 1.0999 0.2111]; and then C=[1.0888 0.7555 0.6444 0.9777 0.8666 0.4222 1.1999 0.3111]; and then C=[1.1888 0.8555 0.7444 1.0777 0.9666 0.5222 1.2999 0.4111]; like this every element of C will cross 1,
i want D matrix to be recording which element first crossed 1 and which element second crossed 1,
like D=[2 5 6 3 4 7 1 8];
Please help i thanks in advance,
Jagadesh Rao Thalur
  2 个评论
Guillaume
Guillaume 2014-11-12
编辑:Guillaume 2014-11-12
Can you edit your question to
  1. format your code properly. Use the {} Code button or put two spaces before each line of code.
  2. Fix the errors in your code so we can run it. Neither S nor C are declared before the start of the loop, so your while statement is invalid. It is doubly invalid because of the = (probably meant ==).
Jagadesh Rao
Jagadesh Rao 2014-11-12
Am sorry Dear Guillaume
A = [0.8888; 0.8555; 0.8444; 0.8777; 0.8666; 0.7222; 0.8999; 0.4111];
C = zeros((size(A,1)),(size(A,2)));
S=0;
B = [0.01; 0.009; 0.008; 0.007; 0.006; 0.005; 0.004; 0.003];
C = A+B;
while S<size(C,1)
C=C+B
S=sum(C(:)>=1);
B=flipud(B)+0.001
end
first C will become
1.0088
0.9745
0.9624
0.9947
0.9826
0.8372
1.0139
0.5241
now i want D to be
1
0
0
0
0
0
1
0
after a while C will become
1.0288
0.9935
0.9804
1.0117
0.9986
0.8522
1.0279
0.5371
now i want D to be
1
0
0
2
0
0
1
0
after that C will become
1.0428
1.0085
0.9964
1.0287
1.0166
0.8712
1.0479
0.5581
i want D to be
1
3
0
2
3
0
1
0
like this i need up to the last C.
I hope now you can easily understand my problem.
THank you so much
Jagadesh

请先登录,再进行评论。

回答(2 个)

Guillaume
Guillaume 2014-11-12
编辑:Guillaume 2014-11-12
I'm not sure why you're using a loop. You can calculate D simply with:
A = [0.8888 0.5555 0.4444 0.7777 0.6666 0.2222 0.9999 0.1111];
B = 0.1;
k = (1-A)./B;
[~, order] = sort(k);
D(order) = 1:numel(A)
  2 个评论
Jagadesh Rao
Jagadesh Rao 2014-11-12
C will not increase like big numbers first, small numbers last.
Sometimes small numbers also can increase and reach 1.
I don't want to record according to big and small numbers, but which reaches value '1' first.
Please try thanks Jagadesh Rao Thalur
Guillaume
Guillaume 2014-11-12
See my comment to your question.
Can you also clarify your comment above. If you continuously add the same constant to some numbers, they will reach 1 in the same order as their starting values (unless they start above one obviously). It can't be any different.
If you're not adding the same constant to all numbers, then change your example to reflect what you're actually doing.

请先登录,再进行评论。


Guillaume
Guillaume 2014-11-13
编辑:Guillaume 2014-11-13
Before the loop initialise D, and a counter
D = zeros(size(A));
counter = 0;
In your loop, after you've calculated C, add
counter = counter + 1;
D = D + (C >= 1 & ~D) * counter;
This will put counter in D if a value in C is above 1 and the corresponding value in D is still 0. That is once a value in C has been recorded in D, it's never going to be assigned a new value.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by