how to arrange data into array

Hi,
i have imported some matrix data into matlab and upon parsing the data i have it in 3 vectors (columns):
column 1 are the row indices (i.e.):
[1;2;3;4]
column 2 are the column indices (i.e.):
[10;11;13;15]
and column 3 are the values in the matrix associated with those indices (i.e.):
[1e-5;5e-5;10e-5;15e-5]
I'd like to arrange this into a single 2-D array of column 3 data in the row locations of column1, and column locations of column2
I could do this with a loop, but im hoping to find a more elegant / less time intensive solution. I have about a million datapoints.
Thanks alot!

댓글 수: 3

James Tursa
James Tursa 2021년 1월 7일
Do you know the desired size of the result up front? Or do you want to just use the max row and column indexes to determine the size of the result?
Tyler
Tyler 2021년 1월 10일
i would use the max row and column indices
Tyler
Tyler 2021년 1월 10일
turns out i can use the sparse() command, followed by the full() command to achieve my desired outcome.

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

답변 (1개)

Stephen23
Stephen23 2021년 1월 10일
편집: Stephen23 2021년 1월 10일

0 개 추천

Without an intermediate sparse array:
R = [1;2;3;4];
C = [10;11;13;15];
V = [1e-5;5e-5;10e-5;15e-5];
%
S = max([R,C],[],1);
M = zeros(S); % or NAN
M(sub2ind(S,R,C)) = V % only the first 10 columns are shown below
M = 4×15
0 0 0 0 0 0 0 0 0 0.0100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0500 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1500

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2016b

질문:

2021년 1월 7일

편집:

2021년 1월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by