# Efficient way of selecting columns of a matrix

조회 수: 1 (최근 30일)
Lutz Köhler 2020년 1월 9일
답변: Stijn Haenen 2020년 1월 9일
Hello everybody,
I am quite new to Matlab and came to the following "problem".
During some for-loops I need to repeatingly select various columns of a matrix. For now I am doing it like this:
cy = cyclePosData(:, cyStart : cyEnd);
The matrix 'cyclePosData' usually has two rows and different number of rows depending on the iteration of the for-loops.
Throughout a method cointaining the for-loop there are multiple of these kind of operations. The line above is called approx. 120 000 times and takes 1.508s. Summed up, these lines with the similar operation take 3.883s. This is defenitely too long for my case.
Question: Is there a more efficient way in Matlab to select a part of a matrix (e.g. columns x to y)?
The code in some context:
for i = 1 : fcyLen - 1
cyEnd = fcyLen - 1;
cyStart = max(1, fcyLen - deltaLen - i + 1);
cy = cyclePosData(:, cyStart : cyEnd);
end
##### 댓글 수: 3이전 댓글 1개 표시이전 댓글 1개 숨기기
Lutz Köhler 2020년 1월 9일
편집: Lutz Köhler 2020년 1월 9일
It is quite a big project, so how much do you need?
The topic is as follows:
I have a database of number x (currently 54) ModelCycles and incoming live Data (Position x and y) and I am looking for the ModelCycle representing the live data the best by calculating a Score based on the distances of each ModelCycle to the live data. Therefore, I move each ModelCycle iteratively over the live data and calculate the difference which is then summed up.
So if I have 2000 data points in my live data and move every of my 54 ModelCycles over it, I need to select different parts of the live data (cut off the beginning and end where there is no ModelCycle data to compare) very often (120 000 times). As a result, it takes my program lots of time to cut down a matrix.
The goal is to do this in realtime. As every 2ms a new datapoint comes in, my current code is way too slow. (1000 datapoints = 2s worth of data)
So if there is no "easy" way to efficiently select a number fo columns, I would need to think about a different approach to calculating a score.
Bob Thompson 2020년 1월 9일
Can you speak more to what kind of data you're getting (numeric, text, etc) and how you're doing your comparison? It seems like the solution might be in your actual comparison logic, rather than in the loop.

댓글을 달려면 로그인하십시오.

### 답변 (1개)

Stijn Haenen 2020년 1월 9일
You can get the data from different columns also in two (maybe more?) ways:
a=[1:10;11:20;21:30];
tic
for i=1:10000
a(:,7:10);
end
toc
tic
for i=1:10000
a(20:30);
end
toc
The second way is 2 to 3 times faster but you get the data in a single array. I dont know if this causes problems in your script.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by