using repmat instead of for loop
7 次查看(过去 30 天)
显示 更早的评论
I have a 1x24 matrix A and a 2000x24 matrix D
I want to take column 1 in A and multiply it by all the rows in column 1 of D, then take the second column of A and multiply it by all the rows in column 2 of D and do that for all 24 columns without using a for loop.
A coworker suggested:
G = D .* repmat(A(1:channels), size(D,1),1);
Hoping G is a 2000x24 matrix. It is, but its not doing what i want it to. Any suggestions?
0 个评论
回答(3 个)
the cyclist
2011-10-13
That should work. But even better is:
>> G = bsxfun(@times,A,D);
If that does not give the answer you expect, I think you need to be a little bit more detailed about what you want, possibly with a small explicit example.
0 个评论
Mason
2011-10-13
2 个评论
the cyclist
2011-10-13
You had errors in both figures 4 and 5. In both cases, you used "shift" instead of "shift-1". In figure 5, you also multiplied by the shift instead of adding. I think you may also have multiplied instead of divided in one place, but I started to get confused, so I basically went back to your figure 1 and 2 code and translated it directly, rather than starting from your figure 4 and 5 code.
I believe the following replicates what you want.
figure(4)
Shifted = bsxfun(@plus,d,shift*(0:channels-1));
plot(Shifted, 'b');% plots pre-normalized values
view(90,90)
figure(5)
normalized = highest_values / highest_value; % matrix of percentages each channel peak value is to the max amplitude value
normalized_signals = bsxfun(@rdivide,d,normalized);% normalized signals on all 24 channels to be plotted
normalized_signals2 = normalized_signals / highest_value;% normalized signals on all 24 channels for imagesc
shifted = bsxfun(@plus,normalized_signals,shift*(0:channels-1))
plot(shifted,'b'); % plots normalized values shifted
view(90,90)
Jan
2011-10-13
For the "clear all" see: http://www.mathworks.com/matlabcentral/answers/16484-good-programming-practice#answer_22301
Mason
2011-10-13
1 个评论
the cyclist
2011-10-13
Glad to help. Next time, rather than adding your own "Answer" as a response, try using the "Comment on this Answer" feature. As this thread currently stands, it is a little bit tricky for you to accept my answer as being helpful, because that help is spread out.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Measurements and Spatial Audio 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!