"Work around" for using an index with decimal entries
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I am writing a master code referencing several separate scripts and did not realize I can NOT use decimals in indices.
My code first asks for some user inputs:
prompt = 'What is the Nominal Depth (in) Parallel to the Flange? ';
C_Nom_D = input(prompt);
prompt = 'What is the Weight in Pounds Per Foot of Length? ';
C_sec = input(prompt);
An example of an entry in the index system I want to use is as follows:
C{10,15.30}=[10.00,2.6,0.24,0.436,4.49,67.4,2.28,0.634,0.796,15.300];
It then calls up the desired information as follows:
M_c = C{C_Nom_D,C_sec};
d = M_c(1);
fprintf('Depth = %.2f\n', d);
b_f = M_c(2);
fprintf('Flange Thickness (in) = %.2f\n', b_f);
This methodology worked when I was using whole numbers, but I now realize that decimals simply do not work.
Does anyone have any suggestions to work around the problem, or perhaps a different way to index my information?
Thanks for any help in advance.
-Jason
0 Commenti
Risposte (2)
Roger Stafford
il 1 Set 2013
If by "decimal" you mean fractions such as 15.30, then no, matlab uses only positive integers for its indices. That is an absolute requirement with indices on this system. There is no "work-around". The function you seemed to have in mind with your expression
C{10,15.30}=[10.00,2.6,0.24,0.436,4.49,67.4,2.28,0.634,0.796,15.300];
would be a 'find' operation in matlab: find the element which is equal to 15.30
4 Commenti
Iain
il 2 Set 2013
Ok, looks like you have two "good" options.
#1 Store the indices in an array of their own, of the same size as the data.
indices = [0 0.1 0.2 0.3 0.4 0.5 0.6 ... etc];
data = [5 5.2 5.2 5.3 5.5 5.9 6.7 ... etc];
dataarr = [5 5.2 5.2 5.3 5.5 5.9 6.7 ... etc;
9 9.4 8.5 7.2 5.1 4.3 2.1 ... ];
And then use the "indices" to find the right element(s), eg.
data(indices == 0.1) %indices exactly equal to 0.1
data(indices - 0.1 < 0.2) % indices within 0.2 of 0.1
dataarr(1:2,indices ==0.1)
#2 Store the indices in an array of its own, of the same size as the data
And then:
data_access = @(x)(interp1(indices, data, x));
data_access(0.1) % interpolates for the value at 0.1
dataarr_access = @(x,r)(interp1(indices, data_arr(r,:), x));
dataarr_access(0.5,1) % interpolates for the value at 0.1
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!