Creating a matrix using the colon function
Ältere Kommentare anzeigen
Hi everyone,
Let us assum that I have two arrays as follows:
A = [1 2 3 4 5]
B = [3 4 5 6 7]
I want to use colon funcion to create Matrix C:
C = [1 2 3
2 3 4
3 4 5
4 5 6
5 6 7]
I used the folloing function, but it does not work. I can use a loop to create this matrix, but I want to know what is the fastest solution. Any idea?
[1 2 3 4 5]: 1: [3 4 5 6 7]
Thanks,
Amir
Akzeptierte Antwort
Weitere Antworten (2)
madhan ravi
am 19 Jul. 2020
1 Stimme
No other fastest way , there’s arrayfun() I could think of but I don’t think any faster. I have also submitted feature request regarding this , let’s see what the future holds.
madhan ravi
am 19 Jul. 2020
ix = max(diff([A;B]));
c = cumsum([A;ones(ix, numel(A))]).'; % if the difference between each A and B elements are the same , this would be the last step
Wanted = c .* (c <= B(:))
1 Kommentar
madhan ravi
am 19 Jul. 2020
Bearbeitet: madhan ravi
am 19 Jul. 2020
You will have to compare the timings , I didn’t check it. Still I bet the loop is the eifficient one.
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!