Add a constant value to a vector’s elements - matlab

I would like to add a constant value of 360 to a vector of values after the maximum value is reached. That is, if H=[12 26 67 92 167 178 112 98 76 85], how do I write a matlab code so that 180 is added to all values after 178? The answer should be H=[12 26 67 92 167 178 292 278 256 265].

This should work on earlier Matlab versions as well:
H=[12 26 67 92 167 178 112 98 76 85]
[n, n] = max(H);
H(n+1:end) = H(n+1:end) + 180

Try following:
n=find(H==max(H));
H(n+1:end)=H(n+1:end)+180;
Since desired vector values are in increasing order, idea here is to find the index of maximum value and increment all the subsequent elements with 180.
EDIT
Better approach for finding max index, as suggested by #LeonidBeschastny
[~,n]=max(H);

Related

setxor single value of cell array with whole cell array values?

My cell array,it contain values like
a=['10100011' '11000111' 00010111' 11100011 '];
I want to apply xor operation ;
I used setxor. I want to xor first value of array i.e 10100011 to all values of cell arrays,required input & output is as follows !
**setxor(10100011, 10100011) =00000000%(first value xor result with first value)
setxor(10100011 , 11000111) =1100100%(first val xor result with second value)
setxor(10100011, 00010111 )=10110100%(first val xor result with third value)
setxor(10100011 ,11100011)=1000000 %(first val xor result with forth value)**
but i don't know how to pass full cell array and single value, i tried to use cellfun but that's not working. I want something that setxor(first val of my cell array , all vals of my cell array) as my cell array is 16x16. Your help would be highly appriciated.
If you're starting from data that looks like this:
A = [163 215 9 131 248 72 246 244 179 33 21 120 153 177 175 249; ...
231 45 77 138 206 76 202 46 82 149 217 30 78 56 68 40];
And you want to XOR the bits of the first entry with every other entry, you don't have to convert A using dec2bin. You can just use bitxor, then format the result however you want (decimal or cell arrays of binary strings):
>> decOut = bitxor(A(1), A)
decOut =
0 116 170 32 91 235 85 87 16 130 182 219 58 18 12 90
68 142 238 41 109 239 105 141 241 54 122 189 237 155 231 139
>> binOut = reshape(cellstr(dec2bin(decOut)), size(A))
binOut =
2×16 cell array
Columns 1 through 10
'00000000' '01110100' '10101010' '00100000' '01011011' '11101011' '01010101' '01010111' '00010000' '10000010'
'01000100' '10001110' '11101110' '00101001' '01101101' '11101111' '01101001' '10001101' '11110001' '00110110'
Columns 11 through 16
'10110110' '11011011' '00111010' '00010010' '00001100' '01011010'
'01111010' '10111101' '11101101' '10011011' '11100111' '10001011'

Importing CSV into Matlab

Chemical composition of a certain material
Hi,
I am trying to import the below mentioned data in CSV format in matlab, which is [1000x10] in dimensions.
HCL;H2SO4;CH4; SULPHUR;CHLORINE;S2O3;SO2;NH3;CO2;O2
144 2 3 141 140 6 7 137 136 10 11 133
13 131 130 16 17 127 126 20 21 123 122 24
25 119 118 28 29 115 114 32 33 111 110 36
108 38 39 105 104 42 43 101 100 46 47 97
96 50 51 93 92 54 55 89 88 58 59 85
61 83 82 64 65 79 78 68 69 75 74 72
73 71 70 76 77 67 66 80 81 63 62 84
60 86 87 57 56 90 91 53 52 94 95 49
48 98 99 45 44 102 103 41 40 106 107 37
109 35 34 112 113 31 30 116 117 27 26 120
121 23 22 124 125 19 18 128 129 15 14 132
12 134 135 9 8 138 139 5 4 142 143 1
I am able to import this data through my code
fid = fopen(uigetfile('.csv'),'rt');
FileName = fopen(fid);
headers = fgets(fid); %get first line
headers = textscan(headers,'%s','delimiter',';'); %read first line
format = repmat('%f',1,size(headers{1,1},1)); %count columns n makeformat string
data = textscan(fid,format,'delimiter',';'); %read rest of the file
data = [data{:}];
I am getting data in matrix form in variable data [1000x10] and name of all the components like HCL, H2SO4 in a cell array named headers{1x1}.
Now I have two questions like the built in import feature in matlab you have flexibility to import data as separate column vectors, numeric matrix,cell array and table format. Is it possible to do as such through code, like i get column vectors with their name HCL with [1000x1] and H2sO4 with [1000x1] in my workspace after import and so on all the column vectors with their names with [1000x1]dimensions.
if yes then help me please...?
If above mentioned is not possible then i can do alternatively that now I have names of column vectors in headers cell array, how I can extract those name and use those names as column vector names through code and I can assign data from data matrix [1000x10] to each column vector with their corresponding names.
like if i say
x = headers {1*1}{1*1}; i will get x = "HCL"
x = genvarname(x); I will get x= x0x22HCL0x2 BUT
I want that x get replaced with HCL.and then I assign
HCL = data(:,1) and same like this other variables H2SO4,SULPHUR, CHLORINE.
You can say i try to implement the import feature of column vector through my code.
Kindly help me to solve this issue. thanks
Have you tried the built-in readtable function?
You can access each column of the table by using the named column header.
If you'd like, you can use the two data types to create a table in MatLab. I'm not terribly familiar with its use, but it seems to be well documented. I'm sure someone else can expand upon this.
Edit:
After re-reading your question, I think this is closer to what you are after.
n=10;
what='HCL';%change this to any of the strings you interested in
numstr = repmat('%f',1,n);
hdrstr = repmat('%s',1,n);
headers = textscan(headers,hdrstr,'delimiter',';');
headers = headers(1,:)
data = cell2mat(textscan(fid,numstr,'delimiter',';'));
datout = data(:,strcmp(headers,what));%datout will be 1000x1 HCL data
Depending on what you want to do, you can loop through these appropriately
I know this is not what you asked for, but I would convert to a struct:
x=cell2struct(num2cell(data),headers,2)
reason is simple, selecting for example the third row with individual variables is not possible. With a struct simply use x(3)
If at some point you need the vectors you originally asked for and you can't use the strcut, use [x.HCL]

MATLAB accessing conditional values and performing operation in single column

Just started MATLAB 2 days ago and I can't figure out a non-loop method (since I read they were slow/inefficient and MATLAB has better alternatives) to perform a simple task.
I have a matrix of 5 columns and 270 rows. What I want to do is:
if the value of an element in column 5 of matrix goodM is below 90, I want to take that element and and subtract it from 90.
So far I tried:
test = goodM(:,5) <= 90;
goodM(test) = 999;
It changes all goodM values within column 1 not 5 into 999, in addition this method doesn't allow me to perform operations on the elements below 90 in column 5. Any elegant solution to doing this?
edit:: goodM(:,5)(test) = 999; doesn't seem to work either so I have no idea to specify the target column.
I am assuming you are looking to operate on elements that have values below 90 as your text in the question reads, rather than 'below or equal to' as represented by '<=' as used in your code. So try this -
ind = find(goodM(:,5) < 90) %// Find indices in column 5 that have values less than 90
goodM(ind,5) = 90 - goodM(ind,5) %// Operate on those elements using indices obtained from previous step
Try this code:
b=90-a(a(:,5)<90,5);
For example:
a =
265 104 479 13 176
26 110 447 208 144
379 163 179 366 464
301 48 274 391 26
429 374 174 184 297
495 375 312 373 82
465 272 399 447 420
205 170 373 122 84
1 417 63 65 252
271 277 412 113 500
then,
b=90-a(a(:,5)<90,5);
b =
64
8
6

find peaks and frequency from spectrum

suppose that we have following picture,which represent power spectral picture
my goal is following:
1.detect peak value of this power spectral picture
2.detect at which frequency it was
first of all,i have got this picture from following command
[Pxx,f]=periodogram(B,[],[],100);
plot(f,Pxx);
where B is input signal and 100 is sampling frequency,i have tried to use findpeaks command in matlab ,like this
[pxx_peaks,location]=findpeaks(Pxx);
and then find
f(location)
but it does not seem to fit to actual frequencies,so please tell me how to find frequencies from given peaks?thanks a lot of
example is following :
peaks are following :
0.417543614272817
0.389922187581014
0.381603315802419
0.601652859233616
0.396925294300794
0.369200511917405
0.477076452316346
0.792431584110476
0.612598437936600
0.564751537228850
0.940538666131177
0.600215481734847
0.985881201195063
0.950077461673118
1.24336273213410
1.84522775800633
1.73186592736729
3.46075557122590
4.93259798197976
8.47095716918618
25.2287636895831
1422.19492782494
60.8238733887811
11.3141744831953
8.65598040591953
3.92785491164888
2.51086405960291
2.27469230188760
1.90435488292485
1.25933693517960
1.52851575480462
0.933543409438383
1.21157308704582
0.821400666720535
1.28706199713640
1.19575886464231
0.736744959694641
0.986899895695809
0.758792061180657
0.542782326712391
0.704787750202814
0.998785634315287
0.522384453408780
0.602294251721841
0.525224294805813
0.624034405807298
0.498659616687732
0.656212420735658
0.866037361133916
0.624405636807668
0.435350646037440
1.22960953802959
0.891793067878849
1.06358076764171
1.34921178081181
1.02878577330537
1.93594290806582
1.14486512201656
2.01004088022982
2.24124811810385
2.15636037453584
4.81721425534142
4.87939454466131
10.5783535493504
27.0572221453758
1490.03057130613
62.3527480644562
13.6074209231800
9.85304975304259
16.3163128995995
74.1532966917877
1510.37374385290
27.7825124315786
8.66382951478539
7.72195587189507
6.06702456628919
3.35353608882459
4.90341095941571
5.07665716731356
4.47635486149688
9.79494608790444
22.9153086380666
1119.97978883924
57.0699524267842
15.2791339483160
5.36617545130941
3.90480316969632
2.58828964019220
1.16385064506181
1.55998411282069
1.14803074836796
0.468260146832541
0.467641715366303
0.698088976126660
0.504713663418641
0.375910057283262
0.331115262928959
0.204555648718379
0.182936666944843
0.293075999812128
0.272993318570981
0.280495615619829
0.148399626645134
location :
3
6
8
11
13
16
18
20
22
25
27
30
32
34
37
39
42
44
46
49
51
55
58
61
63
65
68
70
73
75
77
79
82
85
87
89
91
94
96
99
101
103
106
108
111
113
115
118
120
123
125
127
129
132
134
137
139
141
144
146
148
151
153
156
158
162
165
167
171
174
176
179
183
185
188
190
193
195
197
199
202
204
208
211
213
216
218
220
222
225
227
230
232
234
237
239
241
243
245
248
250
252
254
and f(location)
f(location)
ans =
0.3906
0.9766
1.3672
1.9531
2.3438
2.9297
3.3203
3.7109
4.1016
4.6875
5.0781
5.6641
6.0547
6.4453
7.0313
7.4219
8.0078
8.3984
8.7891
9.3750
9.7656
10.5469
11.1328
11.7188
12.1094
12.5000
13.0859
13.4766
14.0625
14.4531
14.8438
15.2344
15.8203
16.4063
16.7969
17.1875
17.5781
18.1641
18.5547
19.1406
19.5313
19.9219
20.5078
20.8984
21.4844
21.8750
22.2656
22.8516
23.2422
23.8281
24.2188
24.6094
25.0000
25.5859
25.9766
26.5625
26.9531
27.3438
27.9297
28.3203
28.7109
29.2969
29.6875
30.2734
30.6641
31.4453
32.0313
32.4219
33.2031
33.7891
34.1797
34.7656
35.5469
35.9375
36.5234
36.9141
37.5000
37.8906
38.2813
38.6719
39.2578
39.6484
40.4297
41.0156
41.4063
41.9922
42.3828
42.7734
43.1641
43.7500
44.1406
44.7266
45.1172
45.5078
46.0938
46.4844
46.8750
47.2656
47.6563
48.2422
48.6328
49.0234
49.4141
this seems to me like findpeaks does exactly what it should if used with no further parameters it
"finds local peaks in the data vector X. A local peak
is defined as a data sample which is either larger than the two
neighboring samples or is equal to Inf."
(http://www.mathworks.de/de/help/signal/ref/findpeaks.html)
since the only check this function does is testing if a point higher then is neighbours you get a lot of points in a noisy signal
you might want to limit the number of peaks findpeaks returns for example findpeaks(Pxx,'NPEAKS',n) only returns the n bigest peaks or findpeaks(X,'THRESHOLD',t) only returns the peaks which are over the threshold t
the best way might be findpeaks(X,'MINPEAKHEIGHT',m) to look for all peaks which are higher than m and determing m as a percentile of your input Pxx

Selecting elements of a vector based on two vectors of starting and ending positions matlab

I would appreciate your help with the following problem in matlab:
I have a vector and I would like to select parts of it based on the following two vector of start and end indices of parts:
aa = [1 22 41 64 83 105 127 147 170 190 212 233]
bb = [21 40 63 82 104 126 146 169 189 211 232 252]
Basically I would like to perform some function on V(1:21), V(22:40),... V(233:252).
I have tried V(aa:bb) or V(aa(t):bb(t)) where t = 1:12 but I get only V(1:21), probably because V(22:40) has 19 elements compared to V(1:21) which has 22 elements.
Is there a fast way of programming this?
Put your selection in a cell array, and apply your function to each cell:
aa = [1 22 41 64 83 105 127 147 170 190 212 233]
bb = [21 40 63 82 104 126 146 169 189 211 232 252]
V = rand(252,1); % some sample data
selV = arrayfun(#(t) V(aa(t):bb(t)), 1:12,'uniformoutput',false);
result = cellfun(#yourfunction,selV)
% or
result = cellfun(#(selVi) yourfunction(selVi), selV);
If the function you want to apply has scalar output to every vector input, this should give you an 1x12 array. If the function gives vector output, you'll have to include the uniformoutput parameter:
result = cellfun(#(selVi) yourfunction(selVi), selV,'uniformoutput',false);
which gives you a 1x12 cell array.
If you want to run this in a highly condensed form, you can write (in two lines, for clarity)
aa = [1 22 41 64 83 105 127 147 170 190 212 233]
bb = [21 40 63 82 104 126 146 169 189 211 232 252]
V = rand(252,1); % some sample data borrowed from #Gunther
%# create an anonymous function that accepts start/end of range as input
myFunctionHandle = #(low,high)someFunction(V(low:high));
%# calculate result
%# if "someFunction" returns a scalar, you can drop the 'Uni',false part
%# from arrayfun
result = arrayfun(myFunctionHandle(low,high),aa,bb,'uni',false)
Note that this may run more slowly than an explicit loop at the moment, but arrayfun is likely to be multithreaded in a future release.