recording matrix increasing in another matrix.
Show older comments
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 Comments
Can you edit your question to
- format your code properly. Use the {} Code button or put two spaces before each line of code.
- 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
on 12 Nov 2014
Answers (2)
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 Comments
Jagadesh Rao
on 12 Nov 2014
Guillaume
on 12 Nov 2014
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.
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.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!