using repmat instead of for loop
9 views (last 30 days)
Show older comments
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 Comments
Answers (3)
the cyclist
on 13 Oct 2011
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 Comments
Mason
on 13 Oct 2011
2 Comments
the cyclist
on 13 Oct 2011
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
on 13 Oct 2011
For the "clear all" see: http://www.mathworks.com/matlabcentral/answers/16484-good-programming-practice#answer_22301
Mason
on 13 Oct 2011
1 Comment
the cyclist
on 13 Oct 2011
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.
See Also
Categories
Find more on Measurements and Spatial Audio in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!