using repmat instead of for loop

7 views (last 30 days)
Mason
Mason on 13 Oct 2011
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?

Answers (3)

the cyclist
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.

Mason
Mason on 13 Oct 2011
I guess im not understanding something. When i added the bxnfun it did the same thing. Here is what im trying to do. I did it correctly using for loops but im trying to do it now without the for loops.
here is with for loops:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all clc d = ReadSegy('MMAC1.SGY'); % load segy data created in knudsen channels = size(d,2); % total number of channels samples = size(d,1); % total number of samples in time shift=max(max(d))/3 ; % offset shift used to plot seismic data highest_values = max(d); % Builds matrix for highest amplitude of each channel highest_value = max(highest_values); % Largest amplitude of data set
%%%%%%%%%%%%%%%%%%%%%%% Plotting the data as Traces %%%%%%%%%%%%%%%%%%%%%%
figure(1)
for i=1:channels
plot(d(:,i)+shift*(i-1)) % plots pre-normalized values
hold on
view(90,90)
end
figure(2)
normalized_signals = ones(samples(1),channels(1)); % initializes matrix for plotting normalized signals
normalized_signals2 = ones(samples(1),channels(1)); % initializes matrix for imagesc of normalized signals
normalized = highest_values / highest_value; % matrix of percentages each channel peak value is to the max amplitude value
for i=1:channels
normalized_signals(:,i) = d(:,i) ./ normalized(:,i);% normalized signals on all 24 channels to be plotted
normalized_signals2(:,i) = normalized_signals(:,i) / highest_value;% normalized signals on all 24 channels for imagesc
plot(normalized_signals(:,i)+shift*(i-1)); % plots normalized values shifted
hold on
view(90,90)
end
figure(3) % flips origin to top left corner
imagesc(normalized_signals2);
colormap(gray)
colorbar
%%%%%%%%%%%%%%%%%%%end of program %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Here is what i tried after your suggestion:
clear all
clc
d = ReadSegy('MMAC1.SGY'); % load segy data created in knudsen
channels = size(d,2); % total number of channels
samples = size(d,1); % total number of samples in time
shift = max(max(d))/3 ; % offset shift used to plot seismic data
highest_values = max(d); % Builds matrix for highest amplitude of each channel
highest_value = max(highest_values); % Largest amplitude of data set
%%%%%%%%%%%%%%%%%%%%%%% Plotting the data as Traces %%%%%%%%%%%%%%%%%%%%%%
figure(1)
Shifted = d + shift * repmat(1:channels, size(d,1),1 );
plot(Shifted, 'b');% plots pre-normalized values
view(90,90)
figure(2)
fractions_24 = highest_values / highest_value; % Makes a 1x24 matrix of fractions of max amplitudes
scaled = bsxfun(@times,d,fractions_24); % Attempts to take 1st column of fractions_24 and multiply
% it through all rows of the first column of d,
% and do this for all 24 channels.
normalized = scaled / highest_value; % Drops all amplitudes to a max of one rest scaled to thatone
shift = max(max(normalized))/3;
shifted = shift * bsxfun(@times,1:24,normalized);% Shifts all channels to even spacing
plot(normalized) % Plots the normalized waveforms
view(90,90)
figure(3)
imagesc(normalized);
colormap(gray)
colorbar
%%%%%%%%%%%%%%%%%%end of program %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2 Comments
the cyclist
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
Jan on 13 Oct 2011
For the "clear all" see: http://www.mathworks.com/matlabcentral/answers/16484-good-programming-practice#answer_22301

Sign in to comment.


Mason
Mason on 13 Oct 2011
Cyclist, thank you very much. This new function is awesome! I'm trying to do my very best staying away from for loops. You guys rock!
  1 Comment
the cyclist
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!