MATLAB: Multiplication – array by double, vector by double, saving to variable

multiplication

Hi,
I have two similar problems so I decided to put them together in one question.
1. I want to multiply array of randoms by one number from input. I tried cell2mat, extracting element and I still have an error.
*Error using * Inner matrix dimensions must agree.
Error in PL2ex2 (line 26) noise = amplitude{1}*randn(1, length(t));*
function [ noisysum ] = PL2ex2( )
%Gerar função que cria soma de sinusoides com frequências escolhidas pelo
%utilizador e adiciona ruído com amplitude (em dB) escolhida pelo utilizador.
%User's preferences
fprompt = 'Type values of frequencies in form of a vector [f1, f2, f3, ...].';
aprompt = 'Type value of noise amplitude in decibels';
edlg_title = 'Frequencies';
tdlg_title = 'Amplitude';
num_lines = 1;
defa = {'10'};
deff = {'[1, 2, 3]'};
frequencies = cell2mat((inputdlg(fprompt,edlg_title,num_lines,deff)));
amplitude = (inputdlg(aprompt,tdlg_title,num_lines,defa));
%Create sinusoids and sum
fl = length(frequencies);
sinsum = 0;
t = linspace(0,1,1000);
for i = 1:fl
sinsum = sinsum + sin(2*pi*frequencies(i)*t);
end
%add noise
noise = amplitude{1}*randn(1, length(t));
noisysum = sinsum + noise;
figure
plot(t, noisysum)
title('Sum of sinusoids with noise')
xlabel('time')
ylabel('sum values')
2. I need to create sinusoids and save them to one variable.
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in PL3lastex (line 35) sins(k)= A*sin(f(k)*t);
clear; close all;
%Selection
[filename, pathname] = uigetfile('*.mat', 'Select a .mat file containing signal');
if isequal(filename,0)
disp('User selected Cancel')
else
disp(['User selected ', fullfile(pathname, filename)])
end
signal = load(fullfile(pathname, filename));
% Choosing trial
% tprompt = 'Type number of trial (1-61).';
% tdlg_title = 'Trial';
% num_lines = 1;
% deft = {'1'};
% trials = (inputdlg(tprompt,tdlg_title,num_lines,deft));
% eeg_signal = data_org.trial{1}(1,:);
t=linspace(0,100);
A=1;
%fs = 160Hz, freqs belong to 0-80Hz
f = linspace(0.1,79,100);
sins = zeros(1,length(f));
convs = zeros(1,length(f));
amps = zeros(1,length(f));
ffts = zeros(1,length(f));
for k=1:1:length(f);
sins(k)= A*sin(f(k)*t);
convs(k) = conv(signal, sins(k));
amps(k) = sum(abs(convs(k)),2); %2 to work with columns
ffts(k) = fft(sins(k));
end
figure(1)
subplot(1,2,1)
plot(fft(signal))
title('EEG signal spectrum')
subplot(1,2,2)
plot(f, amps(:))
title('Convolutions amplitudes')
Thank you in advance.

Best Answer

What is size(amplitude{1})?
t is a vector, so A*sin(f(k)*t) is a vector, which can't be assigned to a single element sins(k).