在MATLAB中将子数组索引转换为数组索引? [英] Convert subarray index to array index in MATLAB?

查看:82
本文介绍了在MATLAB中将子数组索引转换为数组索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下我有一个数组

A = [12 52 12 62 42];

利用逻辑提取索引" C = logical([1 1 0 1 0]),我形成了一个子数组

With the "logical extraction indices" C = logical([1 1 0 1 0]), I form a subarray

B = A(C);

问题

给定元素在B中的索引为2,那么在A中查找元素的索引的一种优雅方法是什么?

What is an elegant way of finding element's index in A, given its index in B is, say, 2?

推荐答案

这里有两个选择,要么查找B的元素与A的元素匹配的位置,如果A重复,可能会失败,或使用C矩阵,查找第n个.

Here are two options, either look for where the element of B matches an element of A, which could fail if A has repeats, or by using the C matrix, look for the n'th one.

A = [12 52 12 62 42];
C = logical([1 1 0 1 0])
B = A(C)

[~,idx]=find(A==B(3)) %// finds where the third element of B was in A
idx=find(cumsum(C)==3,1) %// finds where the third 1 in C is

这篇关于在MATLAB中将子数组索引转换为数组索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆