Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
MATLAB Vectorization help?
I have the indexes of an array in an array. While that may sound confusing, all I want to do is take the array
[ 1 2 .004
2 3 .02
3 4 .7
2 4 .01
4 5 .006]
and create the array
[0 .004 0 0 0
0 0 .02 0 0
0 0 0 .7 0
0 0 0 0 .006]
elegantly in MATLAB without for loops.
Ha! Thanks, that's a little TOO elegant, this was just an example I made up . . . the array indices are not necessarily along the diagonal, this just happened to be a special case. Say the first row of the first matrix was 1 4 .004 . . . What I'm looking for is a faster version of this
for i=1:length(M(:,1))
L(M(i,1),M(i,2))=M(i,3)
end
Ha! Thanks, that's a little TOO elegant, this was just an example I made up . . . the array indices are not necessarily along the diagonal, this just happened to be a special case. Say the first row of the first matrix was 1 4 .004 . . . What I'm looking for is a faster version of this
for i=1:length(M(:,1))
L(M(i,1),M(i,2))=M(i,3)
end
You're also right in that I didn't mean to delete any of the rows . . .
2 Answers
- R GLv 59 years agoFavorite Answer
a=[ 1 2 .004
2 3 .02
3 4 .7
2 4 .01
4 5 .006];
% cant remember a nicer way without loops
m_size=max(a(:,1:2)); % maximum size of the future matrix
out=zeros(m_size);
ind=sub2ind(m_size,a(:,1), a(:,2));
out(ind)=a(:,3);
- mallieLv 44 years ago
I'll9265cc6a74a5ec7991e3ea2220f9ad1give9265cc6a74a5ec7991e3ea2220f9ad1you9265cc6a74a5ec7991e3ea2220f9ad1a9265cc6a74a5ec7991e3ea2220f9ad1little9265cc6a74a5ec7991e3ea2220f9ad1help...here's9265cc6a74a5ec7991e3ea2220f9ad1an9265cc6a74a5ec7991e3ea2220f9ad1example9265cc6a74a5ec7991e3ea2220f9ad1for9265cc6a74a5ec7991e3ea2220f9ad14.69265cc6a74a5ec7991e3ea2220f9ad1For9265cc6a74a5ec7991e3ea2220f9ad1the9265cc6a74a5ec7991e3ea2220f9ad1items9265cc6a74a5ec7991e3ea2220f9ad1in9265cc6a74a5ec7991e3ea2220f9ad14.69265cc6a74a5ec7991e3ea2220f9ad1you9265cc6a74a5ec7991e3ea2220f9ad1need9265cc6a74a5ec7991e3ea2220f9ad1to9265cc6a74a5ec7991e3ea2220f9ad1understand9265cc6a74a5ec7991e3ea2220f9ad1the9265cc6a74a5ec7991e3ea2220f9ad1notation.9265cc6a74a5ec7991e3ea2220f9ad1for9265cc6a74a5ec7991e3ea2220f9ad1ii=5 10 15 20:5 10 15 20:9265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad1start9265cc6a74a5ec7991e3ea2220f9ad1value:9265cc6a74a5ec7991e3ea2220f9ad1increment9265cc6a74a5ec7991e3ea2220f9ad1value:9265cc6a74a5ec7991e3ea2220f9ad1last9265cc6a74a5ec7991e3ea2220f9ad1value9265cc6a74a5ec7991e3ea2220f9ad1so9265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad1:5 10 15 20:9265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad1would9265cc6a74a5ec7991e3ea2220f9ad1mean9265cc6a74a5ec7991e3ea2220f9ad1start9265cc6a74a5ec7991e3ea2220f9ad1at9265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad1increment9265cc6a74a5ec7991e3ea2220f9ad1by9265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad1until9265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad1or9265cc6a74a5ec7991e3ea2220f9ad1[9265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad19265cc6a74a5ec7991e3ea2220f9ad1]9265cc6a74a5ec7991e3ea2220f9ad1therefore9265cc6a74a5ec7991e3ea2220f9ad1it9265cc6a74a5ec7991e3ea2220f9ad1would9265cc6a74a5ec7991e3ea2220f9ad1loop9265cc6a74a5ec7991e3ea2220f9ad149265cc6a74a5ec7991e3ea2220f9ad1times.