fast and beautiful way to convert vector ranges to indexes

Technical Source
1 min readMar 18, 2023

--

Hi there,

my question could easily be solved with a simple loop but I’m curious if there is a nice genuine matlab way of doing this: I have a vector “v1” containing 10 entries. First 5 belong together, next 2 belong together and the last 3 again (somehow). So I got a vector “v2” with

v2 = [5; 2; 3];

I want now something like

v3 = [1 1 1 1 1 2 2 3 3 3];

So I could access my v1 vector with:

v1(v3==1);

(Background for the question are different colors for each group with the plot-command.)

Thanks already in advance — I’m sure Matlab holds a nice and short way of doing this :-)

NOTE:-

Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

c=cumsum(v2);
v3=zeros(1,c(end));
v3([1,c(1:end-1)])=1;
v3=cumsum(v3);
v3(end)=[],

SEE COMPLETE ANSWER CLICK THE LINK

--

--

Technical Source

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.