Add index matrix to matrix
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
ha ha
am 31 Aug. 2017
Bearbeitet: ha ha
am 2 Sep. 2017
I have some algorithm including a while loop::
while (condition)
.......................................................
.......................................................
.......% do something and return a result array B...
end
Example: I MUST run the code (while...end). For instance, my code will loop 4 time to satify the condition. Therefore, I will get the result of each loop is {B1,B2,B3,B4}
Let's say:
-Loop 1: B1=[1 2 3 4 5....9];
-Loop 2: B2=[10 11 12....15];
-Loop 3: B3=[16 17 18 19] ;
-Loop 4: B4=[20 21 22....30];
The question is: from the result matrix Bi (at each loop):
1/ How to create array index_Bi according to the number of element in array Bi as follows:?
index_B1= [1 1 1 .....1] {9 element 1}
index_B2= [2 2 2.....2] {6 element 2}
index_B3= [3 3 3 3] {4 element 3}
index_B4= [4 4 4.....4] {11 element 4}
_ Note: the number of array "index_Bi" depend on the number of loop (i). In this example, Assumed that the number of loop i=4._
2/ After that, How to add two array (Bi & index_Bi) to matrix A, we will have the result:
A=[ B1 index_B1
B2 index_B2
B3 index_B3
B4 index_B4]
I hope the result of matrix A will be as follows:
A=[ 1 1
2 1
3 1
...1
9 1
10 2
11 2
....2
15 2
16 3
17 3
18 3
19 3
20 4
21 4
.....
30 4]
In the above example, I assume the algorithm is just loop 4 time (i=4). { But actually, For my real data, "the while loop" may be loop up to 100 times (or more). Therefore, i may be equal or larger than 100}*
9 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 1 Sep. 2017
B = [];
ii = 1;
x = [9 6 4 11];
add1 = 0;
while ii <= 4
add1 = add1(end) + (1:x(ii));
B = [B;{add1}];
ii = ii + 1;
end
A = [[B{:}]',repelem((1:numel(B))',cellfun(@numel,B))];
5 Kommentare
Walter Roberson
am 1 Sep. 2017
No, this requires changing your code, which you have indicated is not permitted.
Weitere Antworten (2)
Stephen23
am 1 Sep. 2017
Bearbeitet: Stephen23
am 1 Sep. 2017
You can do this very simply inside the loop, there is no need to use cell arrays, slow cellfun, or repelem.
out = NaN(0,2);
itr = 1;
while ...
tmp = ... your code here
tmp = tmp(:);
tmp(:,2) = itr;
out = [out;tmp];
itr = itr + 1;
end
2 Kommentare
Andrei Bobrov
am 1 Sep. 2017
Bearbeitet: Andrei Bobrov
am 1 Sep. 2017
+1
@ha ha:
A = NaN(0,2);
itr = 1;
while ...
B = ... your code here
B = B(:);
B(:,2) = itr;
A = [A;B];
itr = itr + 1;
end
Walter Roberson
am 1 Sep. 2017
Bearbeitet: Walter Roberson
am 1 Sep. 2017
index_B4= [4 4 4;.....4] {11 element 4}
The above is not possible. The space between the first two "4" tells us that you have multiple columns, and the ';' after the third "4" tells us that you have multiple rows. In MATLAB, each row must be the same size. Therefore, the total number of elements must be divisible by the number of rows. But the total number of elements is 11, which is a prime number; to have that work out, you would need to have only one column per row -- but we see that there a multiple columns per row, so the total number of elements cannot be prime. If there are multiple rows and multiple columns in an array, then the total number of elements must be a composite number, not a prime.
The creating of 11 copies of element 4 is easy:
repmat(4, 1, 11)
However, this requires that you somehow know that the proper length is 11.
Is the length of each subsection somehow known? Is there something particular about the first element of each subsection (for example if the first element were always negative but the others were always positive) ?
6 Kommentare
Walter Roberson
am 1 Sep. 2017
Your restrictions make it impossible to solve the problem.
Suppose, for example, that I told you that the following list of numbers was generated in bursts, and I asked you to identify each burst:
1 5 10 7 9 2 4 3 3 6 8 7 8 7 7 3 1
I can offer the additional information that no burst was negative length, and that no burst had length more than 16383. What were the burst boundaries?
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!