How can I identify value of an array?

2 次查看(过去 30 天)
sc
sc 2018-4-30
重新打开: Guillaume 2018-5-11
Hi! I'm creating a code with Matlab but I've a problem.
I have an array, that is:
  
C = [2,1,0];
I've identified position of values, like:
  
C1 = C (1)
C2 = C (2)
C3 = C (3)
but I'd like to insert an input, that asks to user to insert the different values of array, so I can not know how many values will be in array.
In this case, how can I identify the position of single elements, given an input with a generic number of elements?
I thought to create a 'for cycle' but I have many problems!
Thank you
  7 个评论
sc
sc 2018-4-30
So, I'm creating a code to read and process the dicoms. The code that I created runs and doesn't give problems, but I'd like to change different things according to preferences of user.
For example, I've an array with different values (9 values, expressed with numbers: 0,15,50,100,200,350,500,700,1000) that rapresent the different magnetic fields (in order, 9,1,2,3,4,5,6,7,8).
First, I associated one value to another.
For example: if input is [1000 700 500 350 200 100 50 15 0] I associate these value to their magnetic fields, that in order are [8,7,6,5,4,3,2,1,9].
Now, this runs only if user insert 9 values of input.
I'd like that it runs also with other values, always insert by user, for example 10 values. To do this I'd like to change this passage.
This is a part of code that I want to change:
b = 9;
num_int = numel(files)/b;
for k = 1 : num_int
Ck = [];
for n = 1:b
C_end = C(:,:,K+num_int*(n-1));
Ck = cat(3,Ck,C_end);
end
C_input = [1000,700,500,350,200,100,50,15,0]
C_input_2 = sort(C_input(C_input>0))
Cb0 = C_input(9)
Cb1 = C_input(1)
Cb2 = C_input(2)
Cb3 = C_input(3)
Cb4 = C_input(4)
Cb5 = C_input(5)
Cb6 = C_input(6)
Cb7 = C_input(7)
Cb8 = C_input(8)
Cb0_new = find(C_input == Cb0)
Cb1_new = find(C_input_2 == Cb1)
Cb2_new = find(C_input_2 == Cb2)
Cb3_new = find(C_input_2 == Cb3)
Cb4_new = find(C_input_2 == Cb4)
Cb5_new = find(C_input_2 == Cb5)
Cb6_new = find(C_input_2 == Cb6)
Cb7_new = find(C_input_2 == Cb7)
Cb8_new = find(C_input_2 == Cb8)
C_b0 = Ck(:,:,Cb0_new);
C_b1 = Ck(:,:,Cb1_new);
C_b2 = Ck(:,:,Cb2_new);
C_b3 = Ck(:,:,Cb3_new);
C_b4 = Ck(:,:,Cb4_new);
C_b5 = Ck(:,:,Cb5_new);
C_b6 = Ck(:,:,Cb6_new);
C_b7 = Ck(:,:,Cb7_new);
C_b8 = Ck(:,:,Cb8_new);
Ck = cat(3,C_b0,Ck);
Ck(:,:,10) = [];
eval(['C',num2str(k),'=Ck'])
end
Stephen23
Stephen23 2018-5-2
编辑:Stephen23 2018-5-7
@sc: why do you need to do this? Using indexing is simple and very efficient. What you are trying to do is slow, complex, buggy, and hard to debug. Experienced MATLAB users use indexing: you can too.
EDIT: see also later question, where the OP asks about what they are actually trying to achieve:

请先登录,再进行评论。

采纳的回答

Jan
Jan 2018-4-30
Maybe you want something like this:
C = [2,1,0];
index = find(C == 1);
Or
[match, index] = ismember([1,2], C);
  1 个评论
sc
sc 2018-4-30
I've already done it and the code works; now, I'd like that C is an input with different values and I'd like to create a cycle to find position of each elements in array, but I don't know how to do it because I don't know how many variables the user have put in array, so I'd like to change this function, as like the code will generate it automatically.

请先登录,再进行评论。

更多回答(1 个)

Guillaume
Guillaume 2018-4-30
编辑:Guillaume 2018-4-30
Please, do read the discussion on the link in Dennis' comment. The code you've written is exactly why we say not to number variables and use eval. It's pointless complicated.
Before that,
b = 9;
num_int = numel(files)/b;
for k = 1 : num_int
Ck = [];
for n = 1:b
C_end = C(:,:,K+num_int*(n-1));
Ck = cat(3,Ck,C_end);
end
I'm not entirely sure what you're doing here since K is not defined. Assuming that there is a typo and K is k then all you're doing is deinterlacing data. In which case:
b = 9;
assert(mod(numel(files), b) == 0, 'Number of files must be a multiple of b'); %always a good idea to check your assumptions
num_int = numel(files) / b;
Ck = C(:, :, (1:num_int) + (0:b-1)' * num_int); %requires R2016b
%Ck = C(:, :, bsxfun(@plus, 1:num_int, (0:b-1)' * num_int)); %on earlier versions
This will be much faster than your double loop and your growing Ck array.
Then the whole numbered Cbxx, CBxx_new, etc. is completely pointless. If all you want to do is reorder things, then use indexing:
C_input = [1000,700,500,350,200,100,50,15,0];
order = [9 1 2 3 4 5 6 7 8 9];
new_cb = C_input(order);
C = Ck(:, :, order);
or something like that. Again, I'm really not clear on what you're trying to do. One thing for sure, you do not need numbered variables and eval.
  3 个评论
Guillaume
Guillaume 2018-5-2
I'm afraid I don't really understand what exactly changes with user input and what you want to do with it. An array does not contain variables. Can you describe a bit better what's supposed to change and what calculation should be done as a result.
One thing for sure, even if the size of an array is not fixed, indexing is going to be the solution.
sc
sc 2018-5-7
编辑:sc 2018-5-7
I tried to modify code:
C_input = [8,7,6,5,4,3,2,1,9];
for n = C_input
C_end = C(:,:,k+num_int*(n-1));
Ck = cat(3,Ck,C_end);
end
Now, I'd like that C_input is [1000,700,500,350,200,100,50,15,0] and the code converts this into [8,7,6,5,4,3,2,1,9], that is order position of values (except 0). How can I do?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by