Multiplication - array by double, vector by double, saving to variable

2 views (last 30 days)
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.
  1 Comment
Katarzyna Wieciorek
Katarzyna Wieciorek on 22 Apr 2015
now it works
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;
frequencies = cell2mat((inputdlg(fprompt,edlg_title,num_lines)));
amplitude = (inputdlg(aprompt,tdlg_title,num_lines));
%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 = str2double(amplitude{1})*randn(1, length(t));
noisysum = sinsum + noise;
figure
plot(t, noisysum)
title('Sum of sinusoids with noise')
xlabel('time')
ylabel('sum values')
What about the problem with sinusoids? (Second code)

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 20 Mar 2015
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).
  2 Comments
Katarzyna Wieciorek
Katarzyna Wieciorek on 21 Mar 2015
1. At least according to my assumptions amplitude is input, only one double, as default it is 10. I can't check the size because variables are not exported to workspace because of error (even if I put amplitude in the output next to noisysum). how can I check in different way?
2. I was following this question https://www.mathworks.com/matlabcentral/newsreader/view_thread/283234 but I need to store my sinusoids as well. how to improve variable sins to make it possible?
James Tursa
James Tursa on 21 Mar 2015
You can type the following at the command line before running your program. When you hit an error the program will pause in debug mode and you can check variable sizes etc.
dbstop if error

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!