matlab定义sliced类型,Sliced Variables

秒速五厘米 2023-01-17 08:21 288阅读 0赞

Characteristics of a Sliced Variable

If a variable in a parfor-loop has all the following

characteristics, then the variable is sliced:

Type of First-Level Indexing — The first level of indexing is

either parentheses, (), or braces,

{}.

Fixed Index Listing — Within the first-level parentheses or braces,

the list of indices is the same for all occurrences of a given

variable.

Form of Indexing — Within the list of indices for the variable,

exactly one index involves the loop variable.

Shape of Array — The array maintains a constant shape. In assigning

to a sliced variable, the right side of the assignment cannot be

[] or ‘’, because these operators

attempt to delete elements.

Type of First-Level Indexing. For a sliced variable, the

first level of indexing is enclosed in either parentheses, (), or

braces, {}.

Here are the forms for the first level of indexing for arrays that are sliced and

not sliced.

Not SlicedSlicedA.xA(…)

A.(…)A{…}

After the first level, you can use any type of valid MATLAB® indexing in the second and subsequent levels.

The variable A shown here on the left is not sliced; that shown

on the right is sliced.

A.q{i,12} A{i,12}.q

Fixed Index Listing. Within the first-level indexing of a

sliced variable, the list of indices is the same for all occurrences of a given

variable.

The variable A on the left is not sliced because

A is indexed by i and

i+1 in different places. In the code on the right, variable

A is sliced correctly.

Not slicedSlicedparfor i = 1:k

B(:) = h(A(i), A(i+1));

endparfor i = 1:k

B(:) = f(A(i));

C(:) = g(A{i});

end

The example on the right shows occurrences of first-level indexing using both

parentheses and braces in the same loop, which is acceptable.

The following example on the left does not slice A because the

indexing of A is not the same in all places. The example on the

right slices both A and B. The indexing of

A is not the same as the indexing of B.

However, the indexing of both A and B are

individually consistent.

Not slicedSlicedparfor i=1:10

b = A(1,i) + A(2,i)

endA = [ 1 2 3 4 5 6 7 8 9 10;

10 20 30 40 50 60 70 80 90 100];

B = zeros(1,10);

parfor i=1:10

for n=1:2

B(i) = B(i)+A(n,i)

end

end

Form of Indexing. Within the first-level of indexing for a

sliced variable, exactly one indexing expression is of the form

i, i+k, i-k, or

k+i. The index i is the loop variable and

k is a scalar integer constant or a simple (non-indexed)

broadcast variable. Every other indexing expression is a positive integer constant,

a simple (non-indexed) broadcast variable, a nested for-loop

index variable, colon, or end.

With i as the loop variable, the A variables

shown on the left are not sliced, while the A variables on the

right are sliced.

Not slicedSlicedA(i+f(k),j,:,3) % f(k) invalid for slicing

A(i,20:30,end) % 20:30 not scalar

A(i,:,s.field1) % s.field1 not simple broadcast varA(i+k,j,:,3)

A(i,:,end)

A(i,:,k)

When you use other variables along with the loop variable to index an array, you

cannot set these variables inside the loop. In effect, such variables are constant

over the execution of the entire parfor statement. You cannot

combine the loop variable with itself to form an index expression.

Shape of Array. A sliced variable must maintain a constant

shape. The variable A shown here is not sliced:

A(i,:) = [];

A is not sliced because changing the shape of a sliced array

would violate assumptions governing communication between the client and

workers.

发表评论

表情:
评论列表 (有 0 条评论,288人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Slice详解

    前言 > 切片是一种复合数据类型,与数组类似,存放相同数据类型的元素,但数组的大小是固定的,而切片的大小可变,可以按需自动改变大小。切片是基于底层数组实现的,是对数组的抽

    相关 go slice

    2.2 slice 一个slice是一个数组某个部分的引用。在内存中,它是一个包含3个域的结构体:指向slice中第一个元素的指针,slice的长度,以及slice的容量

    相关 slice

    概念 Slice切片是对底层数组Array的封装,在内存中的存储本质就是数组,体现为连续的内存块,Go语言中的数组定义之后,长度就已经固定了,在使用过程中并不能改变其长度,而

    相关 Rust:slice

    > slice是String的一部分引用。类似切片。 字符串slice 字符串slice:是一些存储在别处的UTF-8编码字符串数据的引用。 slice获取值的使用

    相关 Array.slice()

    slice() 方法可从已有的数组中返回选定的元素。 语法: array.slice(start.end) 该方法并不会修改数组,而是返回一个子数组。如果想删除数